home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / muds / lpmud312.tar / lpmud312 / simulate.c < prev    next >
C/C++ Source or Header  |  1993-01-11  |  73KB  |  2,891 lines

  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <sys/dir.h>
  4. #include <fcntl.h>
  5. #include <setjmp.h>
  6. #include <string.h>
  7. #include <errno.h>
  8. #include <stdio.h>
  9. #include <memory.h>
  10. #if defined(sun)
  11. #include <alloca.h>
  12. #endif
  13. #ifdef M_UNIX
  14. #include <dirent.h>
  15. #endif
  16.  
  17. #include "lint.h"
  18. #include "config.h"
  19. #include "stdio.h"
  20. #include "lang.h"
  21. #include "interpret.h"
  22. #include "object.h"
  23. #include "sent.h"
  24. #include "wiz_list.h"
  25. #include "exec.h"
  26. #include "comm.h"
  27.  
  28. extern int errno;
  29. extern int comp_flag;
  30.  
  31. char *inherit_file;
  32.  
  33. extern int readlink PROT((char *, char *, int));
  34. extern int symlink PROT((char *, char *));
  35. #ifndef MSDOS
  36. #ifndef linux
  37. extern int lstat PROT((char *, struct stat *));
  38. #endif
  39. #else
  40. #define lstat stat
  41. #endif
  42. #ifndef linux
  43. extern int fchmod PROT((int, int));     
  44. #endif
  45. char *last_verb;
  46.  
  47. extern int special_parse PROT((char *)),
  48.     set_call PROT((struct object *, struct sentence *, int)),
  49.     legal_path PROT((char *));
  50.  
  51. void pre_compile PROT((char *)),
  52.     remove_interactive PROT((struct object *)),
  53.     add_light PROT((struct object *, int)),
  54.     add_action PROT((char *, char *, int)),
  55.     add_verb PROT((char *, int)),
  56.     print_local_commands(), ipc_remove(),
  57.     show_info_about PROT((char *, char *, struct interactive *)),
  58.     set_snoop PROT((struct object *, struct object *)),
  59.     print_lnode_status PROT((int)),
  60.     remove_all_players(), start_new_file PROT((FILE *)), end_new_file(),
  61.     move_or_destruct PROT((struct object *, struct object *)),
  62.     load_ob_from_swap PROT((struct object *)), dump_malloc_data(),
  63.     print_svalue PROT((struct svalue *)),
  64.     debug_message_value(),
  65.     destruct2();
  66.  
  67. extern int d_flag;
  68.  
  69. struct object *obj_list, *obj_list_destruct, *master_ob;
  70.  
  71. extern struct wiz_list *back_bone_uid;
  72.  
  73. struct object *current_object;      /* The object interpreting a function. */
  74. struct object *command_giver;       /* Where the current command came from. */
  75. struct object *current_interactive; /* The user who caused this execution */
  76.  
  77. int num_parse_error;        /* Number of errors in the parser. */
  78.  
  79. void shutdowngame();
  80.  
  81. extern void flush_all_player_mess();
  82.  
  83. struct variable *find_status(str, must_find)
  84.     char *str;
  85.     int must_find;
  86. {
  87.     int i;
  88.  
  89.     for (i=0; i < current_object->prog->num_variables; i++) {
  90.     if (strcmp(current_object->prog->variable_names[i].name, str) == 0)
  91.         return ¤t_object->prog->variable_names[i];
  92.     }
  93.     if (!must_find)
  94.     return 0;
  95.     error("--Status %s not found in prog for %s\n", str,
  96.        current_object->name);
  97.     return 0;
  98. }
  99.  
  100. /*
  101.  * Give the correct uid and euid to a created object.
  102.  */
  103. int give_uid_to_object(ob)
  104.     struct object *ob;
  105. {
  106.     struct object *tmp_ob;
  107. #ifdef COMPAT_MODE
  108.     char wiz_name[100];
  109. #else
  110.     struct svalue *ret;
  111.     char *creator_name;
  112. #endif
  113.  
  114.     if (master_ob == 0)
  115.     tmp_ob = ob;
  116.     else {
  117.     assert_master_ob_loaded();
  118.     tmp_ob = master_ob;
  119.     }
  120.     
  121. #ifdef COMPAT_MODE
  122.     /* Is this object wizard defined ? */
  123.     if (sscanf(ob->name, "players/%s", wiz_name) == 1) {
  124.     char *np;
  125.     np = strchr(wiz_name, '/');
  126.     if (np)
  127.         *np = '\0';
  128.     ob->user = add_name(wiz_name);
  129.     } else {
  130.     ob->user = 0;
  131.     }
  132.     ob->eff_user = ob->user;    /* Initial state */
  133.     return 1;
  134. #else
  135.  
  136.     if (!current_object || !current_object->user) {
  137.     /*
  138.      * Only for the master and void object. Note that
  139.      * back_bone_uid is not defined when master.c is being loaded.
  140.      */
  141.     ob->user = add_name("NONAME");
  142.     ob->eff_user = 0;
  143.     return 1;
  144.     }
  145.  
  146.     /*
  147.      * Ask master.c who the creator of this object is.
  148.      */
  149.     push_string(ob->name, STRING_CONSTANT);
  150.     ret = apply("creator_file", tmp_ob, 1);
  151.     if (!ret)
  152.     error("No function 'creator_file' in master.c!\n");
  153.     if (ret->type != T_STRING) {
  154.     struct svalue arg;
  155.     /* This can be the case for objects in /ftp and /open. */
  156.     arg.type = T_OBJECT;
  157.     arg.u.ob = ob;
  158.     destruct_object(&arg);
  159.     error("Illegal object to load.\n");
  160.     }
  161.     creator_name = ret->u.string;
  162.     /*
  163.      * Now we are sure that we have a creator name.
  164.      * Do not call apply() again, because creator_name will be lost !
  165.      */
  166.     if (strcmp(current_object->user->name, creator_name) == 0) {
  167.     /* 
  168.      * The loaded object has the same uid as the loader.
  169.      */
  170.     ob->user = current_object->eff_user;
  171.     ob->eff_user = current_object->eff_user;
  172.     return 1;
  173.     }
  174.  
  175.     if (strcmp(back_bone_uid->name, creator_name) == 0) {
  176.     /*
  177.      * The object is loaded from backbone. This is trusted, so we
  178.      * let it inherit the value of eff_user.
  179.      */
  180.     ob->user = current_object->eff_user;
  181.     ob->eff_user = current_object->eff_user;
  182.     return 1;
  183.     }
  184.  
  185.     /*
  186.      * The object is not loaded from backbone, nor from 
  187.      * from the loading objects path. That should be an object
  188.      * defined by another wizard. It can't be trusted, so we give it the
  189.      * same uid as the creator. Also give it eff_user 0, which means that
  190.      * player 'a' can't use objects from player 'b' to load new objects nor
  191.      * modify files owned by player 'b'.
  192.      *
  193.      * If this effect is wanted, player 'b' must let his object do
  194.      * 'seteuid()' to himself. That is the case for most rooms.
  195.      */
  196.     ob->user = add_name(creator_name);
  197.     ob->eff_user = (struct wiz_list *)0;
  198.     return 1;
  199. #endif /* COMPAT_MODE */
  200. }
  201.  
  202. /*
  203.  * Load an object definition from file. If the object wants to inherit
  204.  * from an object that is not loaded, discard all, load the inherited object,
  205.  * and reload again.
  206.  *
  207.  * In mudlib3.0 when loading inherited objects, their reset() is not called.
  208.  *
  209.  * Save the command_giver, because reset() in the new object might change
  210.  * it.
  211.  *
  212.  * 
  213.  */
  214. struct object *load_object(lname, dont_reset)
  215.     char *lname;
  216.     int dont_reset;
  217. {
  218.     FILE *f;
  219.     extern int total_lines;
  220.     extern int approved_object;
  221.  
  222.     struct object *ob, *save_command_giver = command_giver;
  223.     extern struct program *prog;
  224.     extern char *current_file;
  225.     struct stat c_st;
  226.     int name_length;
  227.     char real_name[200], name[200];
  228.  
  229. #ifndef COMPAT_MODE
  230.     if (current_object && current_object->eff_user == 0)
  231.     error("Can't load objects when no effective user.\n");
  232. #endif
  233.     /* Truncate possible .c in the object name. */
  234.     /* Remove leading '/' if any. */
  235.     while(lname[0] == '/')
  236.     lname++;
  237.     strncpy(name, lname, sizeof(name) - 1);
  238.     name[sizeof name - 1] = '\0';
  239.     name_length = strlen(name);
  240.     if (name_length > sizeof name - 4)
  241.     name_length = sizeof name - 4;
  242.     name[name_length] = '\0';
  243.     if (name[name_length-2] == '.' && name[name_length-1] == 'c') {
  244.     name[name_length-2] = '\0';
  245.     name_length -= 2;
  246.     }
  247.     /*
  248.      * First check that the c-file exists.
  249.      */
  250.     (void)strcpy(real_name, name);
  251.     (void)strcat(real_name, ".c");
  252.     if (stat(real_name, &c_st) == -1) {
  253.     fprintf(stderr, "Could not load descr for %s\n", real_name);
  254.     error("Failed to load file.\n");
  255.     return 0;
  256.     }
  257.     /*
  258.      * Check if it's a legal name.
  259.      */
  260.     if (!legal_path(real_name)) {
  261.     fprintf(stderr, "Illegal pathname: %s\n", real_name);
  262.     error("Illegal path name.\n");
  263.     return 0;
  264.     }
  265.     if (comp_flag)
  266.     fprintf(stderr, " compiling %s ...", real_name);
  267.     f = fopen(real_name, "r");
  268.     if (f == 0) {
  269.     perror(real_name);
  270.     error("Could not read the file.\n");
  271.     }
  272.     start_new_file(f);
  273.     current_file = string_copy(real_name);    /* This one is freed below */
  274.     compile_file();
  275.     end_new_file();
  276.     if (comp_flag)
  277.         fprintf(stderr, " done\n");
  278.     update_compile_av(total_lines);
  279.     total_lines = 0;
  280.     (void)fclose(f);
  281.     free(current_file);
  282.     current_file = 0;
  283.     /* Sorry, can't handle objects without programs yet. */
  284.     if (inherit_file == 0 && (num_parse_error > 0 || prog == 0)) {
  285.     if (prog)
  286.         free_prog(prog, 1);
  287.     if (num_parse_error == 0 && prog == 0)
  288.         error("No program in object !\n");
  289.     error("Error in loading object\n");
  290.     }
  291.     /*
  292.      * This is an iterative process. If this object wants to inherit an
  293.      * unloaded object, then discard current object, load the object to be
  294.      * inherited and reload the current object again. The global variable
  295.      * "inherit_file" will be set by lang.y to point to a file name.
  296.      */
  297.     if (inherit_file) {
  298.     char *tmp = inherit_file;
  299.     if (prog) {
  300.         free_prog(prog, 1);
  301.         prog = 0;
  302.     }
  303.     if (strcmp(inherit_file, name) == 0) {
  304.         free(inherit_file);
  305.         inherit_file = 0;
  306.         error("Illegal to inherit self.\n");
  307.     }
  308.     inherit_file = 0;
  309. #if 1 /* MUDLIB3_NEED, It's very awkard to have to have a debug3 /JnA */
  310. #ifdef COMPAT_MODE
  311.     load_object(tmp, 0);
  312. #else    
  313.     load_object(tmp, 1);
  314. #endif
  315. #else
  316.     load_object(tmp, 0);        /* Remove this feature for now */
  317. #endif
  318.     free(tmp);
  319.     ob = load_object(name, dont_reset);
  320.     return ob;
  321.     }
  322.     ob = get_empty_object(prog->num_variables);
  323.     /*
  324.      * Can we approve of this object ?
  325.      */
  326.     if (approved_object || strcmp(prog->name, "std/object.c") == 0)
  327.     ob->flags |= O_APPROVED;
  328.     ob->name = string_copy(name);    /* Shared string is no good here */
  329.     ob->prog = prog;
  330.     ob->next_all = obj_list;
  331.     obj_list = ob;
  332.     enter_object_hash(ob);    /* add name to fast object lookup table */
  333.  
  334.     if (give_uid_to_object(ob) && !dont_reset)
  335.     reset_object(ob, 0);
  336.     if (!(ob->flags & O_DESTRUCTED) && function_exists("clean_up",ob)) {
  337.     ob->flags |= O_WILL_CLEAN_UP;
  338.     }
  339.     command_giver = save_command_giver;
  340.     if (d_flag > 1 && ob)
  341.     debug_message("--%s loaded\n", ob->name);
  342.     return ob;
  343. }
  344.  
  345. char *make_new_name(str)
  346.     char *str;
  347. {
  348.     static int i;
  349.     char *p = xalloc(strlen(str) + 10);
  350.  
  351.     (void)sprintf(p, "%s#%d", str, i);
  352.     i++;
  353.     return p;
  354. }
  355.     
  356.  
  357. /*
  358.  * Save the command_giver, because reset() in the new object might change
  359.  * it.
  360.  */
  361. struct object *clone_object(str1)
  362.     char *str1;
  363. {
  364.     struct object *ob, *new_ob;
  365.     struct object *save_command_giver = command_giver;
  366.  
  367. #ifndef COMPAT_MODE
  368.     if (current_object && current_object->eff_user == 0)
  369.     error("Illegal to call clone_object() with effective user 0\n");
  370. #endif
  371.     ob = find_object(str1);
  372.     /*
  373.      * If the object self-destructed...
  374.      */
  375.     if (ob == 0)
  376.     return 0;
  377.     if (ob->super || (ob->flags & O_CLONE))
  378.     error("Cloning a bad object !\n");
  379.     
  380.     /* We do not want the heart beat to be running for unused copied objects */
  381.  
  382.     if (ob->flags & O_HEART_BEAT) 
  383.     (void)set_heart_beat(ob, 0);
  384.     new_ob = get_empty_object(ob->prog->num_variables);
  385.     new_ob->name = make_new_name(ob->name);
  386.     new_ob->flags |= O_CLONE | ob->flags & ( O_APPROVED | O_WILL_CLEAN_UP ) ;
  387. #if 0
  388.     if (ob->flags & O_APPROVED)
  389.     new_ob->flags |= O_APPROVED;
  390. #endif
  391.     new_ob->prog = ob->prog;
  392.     reference_prog (ob->prog, "clone_object");
  393. #ifdef COMPAT_MODE
  394.     if (current_object && current_object->user && !ob->user)
  395.     new_ob->user = current_object->user;
  396.     else
  397.     new_ob->user = ob->user;        /* Possibly a null pointer */
  398.     new_ob->eff_user = new_ob->user;    /* Init state */
  399. #else 
  400.     if (!current_object)
  401.     fatal("clone_object() from no current_object !\n");
  402.     
  403.     give_uid_to_object(new_ob);
  404.  
  405. #endif
  406.     new_ob->next_all = obj_list;
  407.     obj_list = new_ob;
  408.     enter_object_hash(new_ob);    /* Add name to fast object lookup table */
  409.     reset_object(new_ob, 0); 
  410.     command_giver = save_command_giver;
  411.     /* Never know what can happen ! :-( */
  412.     if (new_ob->flags & O_DESTRUCTED)
  413.     return 0;
  414.     return new_ob;
  415. }
  416.  
  417. struct object *environment(arg)
  418.     struct svalue *arg;
  419. {
  420.     struct object *ob = current_object;
  421.  
  422.     if (arg && arg->type == T_OBJECT)
  423.     ob = arg->u.ob;
  424.     else if (arg && arg->type == T_STRING)
  425.     ob = find_object2(arg->u.string);
  426.     if (ob == 0 || ob->super == 0 || (ob->flags & O_DESTRUCTED))
  427.     return 0;
  428.     if (ob->flags & O_DESTRUCTED)
  429.     error("environment() off destructed object.\n");
  430.     return ob->super;
  431. }
  432.  
  433. /*
  434.  * Execute a command for an object. Copy the command into a
  435.  * new buffer, because 'parse_command()' can modify the command.
  436.  * If the object is not current object, static functions will not
  437.  * be executed. This will prevent forcing players to do illegal things.
  438.  *
  439.  * Return cost of the command executed if success (> 0).
  440.  * When failure, return 0.
  441.  */
  442. int command_for_object(str, ob)
  443.     char *str;
  444.     struct object *ob;
  445. {
  446.     char buff[1000];
  447.     extern int eval_cost;
  448.     int save_eval_cost = eval_cost - 1000;
  449.  
  450.     if (strlen(str) > sizeof(buff) - 1)
  451.     error("Too long command.\n");
  452.     if (ob == 0)
  453.     ob = current_object;
  454.     else if (ob->flags & O_DESTRUCTED)
  455.     return 0;
  456.     strncpy(buff, str, sizeof buff);
  457.     buff[sizeof buff - 1] = '\0';
  458.     if (parse_command(buff, ob))
  459.     return eval_cost - save_eval_cost;
  460.     else
  461.     return 0;
  462. }
  463.  
  464. /*
  465.  * To find if an object is present, we have to look in two inventory
  466.  * lists. The first list is the inventory of the current object.
  467.  * The second list is all things that have the same ->super as
  468.  * current_object.
  469.  * Also test the environment.
  470.  * If the second argument 'ob' is non zero, only search in the
  471.  * inventory of 'ob'. The argument 'ob' will be mandatory, later.
  472.  */
  473.  
  474. static struct object *object_present2 PROT((char *, struct object *));
  475.  
  476. struct object *object_present(v, ob)
  477.     struct svalue *v;
  478.     struct object *ob;
  479. {
  480.     struct svalue *ret;
  481.     struct object *ret_ob;
  482.     int specific = 0;
  483.  
  484.     if (ob == 0)
  485.     ob = current_object;
  486.     else
  487.     specific = 1;
  488.     if (ob->flags & O_DESTRUCTED)
  489.     return 0;
  490.     if (v->type == T_OBJECT) {
  491.     if (specific) {
  492.         if (v->u.ob->super == ob)
  493.         return v->u.ob;
  494.         else
  495.         return 0;
  496.     }
  497.     if (v->u.ob->super == ob ||
  498.         (v->u.ob->super == ob->super && ob->super != 0))
  499.         return v->u.ob->super;
  500.     return 0;
  501.     }
  502.     ret_ob = object_present2(v->u.string, ob->contains);
  503.     if (ret_ob)
  504.     return ret_ob;
  505.     if (specific)
  506.     return 0;
  507.     if (ob->super) {
  508.     push_string(v->u.string, STRING_CONSTANT);
  509.     ret = apply("id", ob->super, 1);
  510.     if (ob->super->flags & O_DESTRUCTED)
  511.         return 0;
  512.     if (ret && !(ret->type == T_NUMBER && ret->u.number == 0))
  513.         return ob->super;
  514.     return object_present2(v->u.string, ob->super->contains);
  515.     }
  516.     return 0;
  517. }
  518.  
  519. static struct object *object_present2(str, ob)
  520.     char *str;
  521.     struct object *ob;
  522. {
  523.     struct svalue *ret;
  524.     char *p;
  525.     int count = 0, length;
  526.     char *item;
  527.  
  528.     item = string_copy(str);
  529.     length = strlen(item);
  530.     p = item + length - 1;
  531.     if (*p >= '0' && *p <= '9') {
  532.     while(p > item && *p >= '0' && *p <= '9')
  533.         p--;
  534.     if (p > item && *p == ' ') {
  535.         count = atoi(p+1) - 1;
  536.         *p = '\0';
  537.         length = p - item;    /* This is never used again ! */
  538.     }
  539.     }
  540.     for (; ob; ob = ob->next_inv) {
  541.     push_string(item, STRING_CONSTANT);
  542.     ret = apply("id", ob, 1);
  543.     if (ob->flags & O_DESTRUCTED) {
  544.         free(item);
  545.         return 0;
  546.     }
  547.     if (ret == 0 || (ret->type == T_NUMBER && ret->u.number == 0))
  548.         continue;
  549.     if (count-- > 0)
  550.         continue;
  551.     free(item);
  552.     return ob;
  553.     }
  554.     free(item);
  555.     return 0;
  556. }
  557.  
  558. /*
  559.  * Remove an object. It is first moved into the destruct list, and
  560.  * not really destructed until later. (see destruct2()).
  561.  */
  562. void destruct_object(v)
  563.     struct svalue *v;
  564. {
  565.     struct object *ob, *super;
  566.     struct object **pp;
  567.     int removed;
  568.  
  569.     if (v->type == T_OBJECT)
  570.     ob = v->u.ob;
  571.     else {
  572.     ob = find_object2(v->u.string);
  573.     if (ob == 0)
  574.         error("destruct_object: Could not find %s\n", v->u.string);
  575.     }
  576.     if (ob->flags & O_DESTRUCTED)
  577.     return;
  578.     if (ob->flags & O_SWAPPED)
  579.     load_ob_from_swap(ob);
  580.     remove_object_from_stack(ob);
  581.     /*
  582.      * If this is the first object being shadowed by another object, then
  583.      * destruct the whole list of shadows.
  584.      */
  585.     if (ob->shadowed && !ob->shadowing) {
  586.     struct svalue svp;
  587.     struct object *ob2;
  588.  
  589.     svp.type = T_OBJECT;
  590.     for (ob2 = ob->shadowed; ob2; ) {
  591.         svp.u.ob = ob2;
  592.         ob2 = ob2->shadowed;
  593.         svp.u.ob->shadowed = 0;
  594.         svp.u.ob->shadowing = 0;
  595.         destruct_object(&svp);
  596.     }
  597.     }
  598.     /*
  599.      * The chain of shadows is a double linked list. Take care to update
  600.      * it correctly.
  601.      */
  602.     if (ob->shadowing)
  603.     ob->shadowing->shadowed = ob->shadowed;
  604.     if (ob->shadowed)
  605.     ob->shadowed->shadowing = ob->shadowing;
  606.     ob->shadowing = 0;
  607.     ob->shadowed = 0;
  608.  
  609.     if (d_flag > 1)
  610.     debug_message("Destruct object %s (ref %d)\n", ob->name, ob->ref);
  611.     super = ob->super;
  612.     if (super) {
  613. #ifdef COMPAT_MODE
  614.     struct svalue *weight;
  615.     /* Call exit in current room, if player or npc not in mudlib 3.0 */
  616.     if((ob->flags & O_ENABLE_COMMANDS)) {
  617.         push_object(ob);
  618.         (void)apply("exit",super,1);
  619.     }
  620.     weight = apply("query_weight", ob, 0);
  621.     if (weight && weight->type == T_NUMBER) {
  622.         push_number(-weight->u.number);
  623.         (void)apply("add_weight", super, 1);
  624.     }
  625. #endif
  626.     }
  627.     if (super == 0) {
  628.     /*
  629.      * There is nowhere to move the objects.
  630.      */
  631.     struct svalue svp;
  632.     svp.type = T_OBJECT;
  633.     while(ob->contains) {
  634.         svp.u.ob = ob->contains;
  635.         push_object(ob->contains);
  636.         /* An error here will not leave destruct() in an inconsistent
  637.          * stage.
  638.          */
  639.         apply_master_ob("destruct_environment_of",1);
  640.         if (svp.u.ob == ob->contains)
  641.         destruct_object(&svp);
  642.     }
  643.     } else {
  644.     while(ob->contains)
  645.         move_or_destruct(ob->contains, super);
  646.     }
  647.     if ( ob->interactive ) {
  648.     struct object *save=command_giver;
  649.  
  650.     command_giver=ob;
  651.     if (ob->interactive->ed_buffer) {
  652.         extern void save_ed_buffer();
  653.  
  654.         save_ed_buffer();
  655.     }
  656.     flush_all_player_mess();
  657.     command_giver=save;
  658.     }
  659.     set_heart_beat(ob, 0);
  660.     /*
  661.      * Remove us out of this current room (if any).
  662.      * Remove all sentences defined by this object from all objects here.
  663.      */
  664.     if (ob->super) {
  665.     if (ob->super->flags & O_ENABLE_COMMANDS)
  666.         remove_sent(ob, ob->super);
  667.     add_light(ob->super, - ob->total_light);
  668.     for (pp = &ob->super->contains; *pp;) {
  669.         if ((*pp)->flags & O_ENABLE_COMMANDS)
  670.         remove_sent(ob, *pp);
  671.         if (*pp != ob)
  672.         pp = &(*pp)->next_inv;
  673.         else
  674.         *pp = (*pp)->next_inv;
  675.     }
  676.     }
  677.     /*
  678.      * Now remove us out of the list of all objects.
  679.      * This must be done last, because an error in the above code would
  680.      * halt execution.
  681.      */
  682.     removed = 0;
  683.     for (pp = &obj_list; *pp; pp = &(*pp)->next_all) {
  684.     if (*pp != ob)
  685.         continue;
  686.     *pp = (*pp)->next_all;
  687.     removed = 1;
  688.     remove_object_hash(ob);
  689.     break;
  690.     }
  691.     if (!removed)
  692.         fatal("Failed to delete object.\n");
  693.     if (ob->living_name)
  694.     remove_living_name(ob);
  695.     ob->super = 0;
  696.     ob->next_inv = 0;
  697.     ob->contains = 0;
  698.     ob->flags &= ~O_ENABLE_COMMANDS;
  699.     ob->next_all = obj_list_destruct;
  700.     obj_list_destruct = ob;
  701.     ob->flags |= O_DESTRUCTED;
  702. }
  703.  
  704. /*
  705.  * This one is called when no program is executing from the main loop.
  706.  */
  707. void destruct2(ob)
  708.     struct object *ob;
  709. {
  710.     if (d_flag > 1) {
  711.     debug_message("Destruct-2 object %s (ref %d)\n", ob->name, ob->ref);
  712.     }
  713.     if (ob->interactive)
  714.     remove_interactive(ob);
  715.     /*
  716.      * We must deallocate variables here, not in 'free_object()'.
  717.      * That is because one of the local variables may point to this object,
  718.      * and deallocation of this pointer will also decrease the reference
  719.      * count of this object. Otherwise, an object with a variable pointing
  720.      * to itself, would never be freed.
  721.      * Just in case the program in this object would continue to
  722.      * execute, change string and object variables into the number 0.
  723.      */
  724.     if (ob->prog->num_variables > 0) {
  725.     /*
  726.      * Deallocate variables in this object.
  727.      * The space of the variables are not deallocated until
  728.      * the object structure is freed in free_object().
  729.      */
  730.     int i;
  731.     for (i=0; i<ob->prog->num_variables; i++) {
  732.         free_svalue(&ob->variables[i]);
  733.         ob->variables[i].type = T_NUMBER;
  734.         ob->variables[i].u.number = 0;
  735.     }
  736.     }
  737.     free_object(ob, "destruct_object");
  738. }
  739.  
  740. #ifdef F_CREATE_WIZARD
  741. /*
  742.  * This is the efun create_wizard(). Create a home dir for a wizard,
  743.  * and other files he may want.
  744.  *
  745.  * The real job is done by the master.c object, so the call could as well
  746.  * have been done to it directly. But, this function remains for
  747.  * compatibility.
  748.  *
  749.  * It should be replaced by a function in simul_efun.c !
  750.  */
  751. char *create_wizard(owner, domain)
  752.     char *owner;
  753.     char *domain;
  754. {
  755.     struct svalue *ret;
  756. #if 0
  757.     struct stat st;
  758.     FILE *f;
  759.     char cmd[200], lbuf[128];
  760.     static char name[200], name2[200];    /* Ugly fix /Lars (static) */
  761.     struct object *owner_obj;
  762. #endif
  763.  
  764.     /*
  765.      * Let the master object do the job.
  766.      */
  767.     push_constant_string(owner);
  768.     push_constant_string(domain);
  769.     push_object(current_object);
  770.     ret = apply_master_ob("master_create_wizard", 3);
  771.     if (ret && ret->type == T_STRING)
  772.     return ret->u.string;
  773.     return 0;
  774. #if 0
  775.     /*
  776.      * Verify that it is a valid call of create_wizard(). This is done
  777.      * by a function in master.c. It will take the calling object as
  778.      * argument, and must return a non-zero value.
  779.      */
  780.     push_object(current_object);
  781.     ret = apply_master_ob("verify_create_wizard", 1);
  782.     if (ret == 0)
  783.     error("No wizards allowed ! (because no verify_create_wizard() in master.c)\n");
  784.     if (ret->type == T_NUMBER && ret->u.number == 0)
  785.     error("Illegal use of create_wizard() !\n");
  786.  
  787.     /*
  788.      * Even if the object that called create_wizard() is trusted, we won't
  789.      * allow it to use funny names for the owner.
  790.      */
  791.     if (!legal_path(owner))
  792.     error("Bad name to create_wizard: %s\n", owner);
  793.  
  794.     owner_obj = find_living_object(owner, 1);
  795.     if (owner_obj == 0) {
  796.     fprintf(stderr,
  797.         "create_wizard: Could not find living object %s.\n", owner);
  798.     return 0;
  799.     }
  800.  
  801.     /*
  802.      * Create the path to wizards home directory.
  803.      */
  804.     (void)sprintf(name, "%s/%s", PLAYER_DIR, owner);
  805.  
  806.     /*
  807.      * If we are using domains, make a path to the domain.
  808.      */
  809.     if(domain) {
  810.     (void)sprintf(name2, "%s/%s/%s", DOMAIN_DIR, domain, owner);
  811.     fprintf(stderr, "name = %s, name2 =  %s\n", name, name2);
  812.  
  813.     /*
  814.      * If the directory already exists, we move it to the domain.
  815.      */
  816.     if (stat(name, &st) == 0) {
  817.         if((st.st_mode & S_IFMT) == S_IFDIR) {
  818.         rename(name, name2);
  819.         }
  820.     } else {
  821.         if (mkdir(name2, 0777) == -1) {
  822.         perror(name2);
  823.         error("Could not mkdir %s\n", name2);
  824.         }
  825.     }
  826.     } else {
  827.     if (stat(name, &st) == 0)
  828.         error("Player %s already has a castle!\n", owner);
  829.  
  830.     else
  831.         if (mkdir(name, 0777) == -1) {
  832.         perror(name);
  833.         error("Could not mkdir %s\n", name);
  834.         }
  835.     }
  836.  
  837.     /* add castle */
  838.     if(domain) {
  839.     (void)sprintf(name, "%s/%s/common/domain.c", DOMAIN_DIR, domain);
  840.     } else {
  841.     (void)sprintf(name, "%s/%s/%s", PLAYER_DIR, owner, "castle.c");
  842.     }
  843.     if(stat(name, &st) == 0) {
  844.     fprintf(stderr, "castle file %s already exists.\n", name);
  845.     } else {
  846.     f = fopen(name, "w");
  847.     if (f == NULL)
  848.         error("Could not create a castle file %s!\n", name);
  849.     (void)fprintf(f, "#define NAME \"%s\"\n", domain ? domain : owner);
  850. #ifdef CASTLE_ROOM
  851.     (void)fprintf(f, "#define DEST \"%s\"\n", CASTLE_ROOM);
  852. #else
  853.     (void)fprintf(f, "#define DEST \"%s\"\n",
  854.               current_object->super->name);
  855. #endif
  856.     (void)fclose(f);
  857.     (void)sprintf(cmd, "cat %s >> %s", DEFAULT_CASTLE, name);
  858.     (void)system(cmd);
  859.     }
  860.     
  861.     /*
  862.      * Add this castle name to the list of files to be loaded.
  863.      */
  864.     f = fopen(INIT_FILE, "a");
  865.     if (f == NULL)
  866.     error("Could not add the new castle to the %s\n", INIT_FILE);
  867.     (void)fprintf(f, "%s\n", name);
  868.     (void)fclose(f);
  869.     return name;
  870. #endif
  871. }
  872. #endif /* F_CREATE_WIZARD */
  873.  
  874. /*
  875.  * A message from an object will reach
  876.  * all objects in the inventory,
  877.  * all objects in the same environment and
  878.  * the surrounding object.
  879.  * Non interactive objects gets no messages.
  880.  *
  881.  * There are two cases to take care of. If this routine is called from
  882.  * a player (indirectly), then the message goes to all in his
  883.  * environment. Otherwise, the message goes to all in the current_object's
  884.  * environment (as the case when called from a heart_beat()).
  885.  *
  886.  * If there is a second argument 'avoid_ob', then do not send the message
  887.  * to that object.
  888.  */
  889.  
  890. void say(v, avoid)
  891.     struct svalue *v;
  892.     struct vector *avoid;
  893. {
  894.     extern struct vector *order_alist PROT((struct vector *));
  895.     struct vector *vtmpp;
  896.     static struct vector vtmp = { 1, 1,
  897. #ifdef DEBUG
  898.     1,
  899. #endif
  900.     (struct wiz_list *)NULL,
  901.     { { T_POINTER } }
  902.     };
  903.  
  904.     extern int assoc PROT((struct svalue *key, struct vector *));
  905.     struct object *ob, *save_command_giver = command_giver;
  906.     struct object *origin;
  907.     char buff[256];
  908. #define INITIAL_MAX_RECIPIENTS 50
  909.     int max_recipients = INITIAL_MAX_RECIPIENTS;
  910.     struct object *first_recipients[INITIAL_MAX_RECIPIENTS];
  911.     struct object **recipients = first_recipients;
  912.     struct object **curr_recipient = first_recipients;
  913.     struct object **last_recipients =
  914.     &first_recipients[INITIAL_MAX_RECIPIENTS-1];
  915.  
  916.     struct object *save_again;
  917.     static struct svalue stmp = { T_OBJECT };
  918.  
  919.     if (current_object->flags & O_ENABLE_COMMANDS)
  920.     command_giver = current_object;
  921.     else if (current_object->shadowing)
  922.     command_giver = current_object->shadowing;
  923.     if (command_giver) {
  924.     origin = command_giver;
  925.         if (avoid->item[0].type == T_NUMBER) {
  926.             avoid->item[0].type = T_OBJECT;
  927.             avoid->item[0].u.ob = command_giver;
  928.             add_ref(command_giver, "ass to var");
  929.         }
  930.     } else
  931.     origin = current_object;
  932.     vtmp.item[0].u.vec = avoid;
  933.     vtmpp = order_alist(&vtmp);
  934.     avoid = vtmpp->item[0].u.vec;
  935.     if (ob = origin->super) {
  936.     if (ob->flags & O_ENABLE_COMMANDS || ob->interactive) {
  937.         *curr_recipient++ = ob;
  938.     }
  939.     for (ob = origin->super->contains; ob; ob = ob->next_inv) {
  940.             if (ob->flags & O_ENABLE_COMMANDS || ob->interactive) {
  941.                 if (curr_recipient >= last_recipients) {
  942.                     max_recipients <<= 1;
  943.                     curr_recipient = (struct object **)
  944.               alloca(max_recipients * sizeof(struct object *));
  945.                     memcpy((char*)curr_recipient, (char*)recipients,
  946.                       max_recipients * sizeof(struct object *)>>1);
  947.                     recipients = curr_recipient;
  948.                     last_recipients = &recipients[max_recipients-1];
  949.             curr_recipient += (max_recipients>>1) - 1;
  950.                 }
  951.                 *curr_recipient++ = ob;
  952.             }
  953.     }
  954.     }
  955.     for (ob = origin->contains; ob; ob = ob->next_inv) {
  956.     if (ob->flags & O_ENABLE_COMMANDS || ob->interactive) {
  957.         if (curr_recipient >= last_recipients) {
  958.         max_recipients <<= 1;
  959.         curr_recipient = (struct object **)alloca(max_recipients);
  960.         memcpy((char*)curr_recipient, (char*)recipients,
  961.           max_recipients * sizeof(struct object *)>>1);
  962.         recipients = curr_recipient;
  963.         last_recipients = &recipients[max_recipients-1];
  964.         curr_recipient += (max_recipients>>1) - 1;
  965.         }
  966.         *curr_recipient++ = ob;
  967.     }
  968.     }
  969.     *curr_recipient = (struct object *)0;
  970.     switch(v->type) {
  971.     case T_STRING:
  972.     strncpy(buff, v->u.string, sizeof buff);
  973.     buff[sizeof buff - 1] = '\0';
  974.     break;
  975.     case T_OBJECT:
  976.     strncpy(buff, v->u.ob->name, sizeof buff);
  977.     buff[sizeof buff - 1] = '\0';
  978.     break;
  979.     case T_NUMBER:
  980.     sprintf(buff, "%d", v->u.number);
  981.     break;
  982.     case T_POINTER:
  983.     for (curr_recipient = recipients; ob = *curr_recipient++; ) {
  984.         extern void push_vector PROT((struct vector *));
  985.  
  986.         if (ob->flags & O_DESTRUCTED) continue;
  987.         stmp.u.ob = ob;
  988.         if (assoc(&stmp, avoid) >= 0) continue;
  989.         push_vector(v->u.vec);
  990.         push_object(command_giver);
  991.         apply("catch_msg", ob, 2);
  992.     }
  993.     break;
  994.     default:
  995.     error("Invalid argument %d to say()\n", v->type);
  996.     }
  997.     save_again = command_giver;
  998.     for (curr_recipient = recipients; ob = *curr_recipient++; ) {
  999.         if (ob->flags & O_DESTRUCTED) continue;
  1000.     stmp.u.ob = ob;
  1001.     if (assoc(&stmp, avoid) >= 0) continue;
  1002.     if (ob->interactive == 0) {
  1003.         tell_npc(ob, buff);
  1004.         continue;
  1005.     }
  1006.     command_giver = ob;
  1007.     add_message("%s", buff);
  1008.     command_giver = save_again;
  1009.     }
  1010.     free_vector(vtmpp);
  1011.     command_giver = save_command_giver;
  1012. }
  1013.  
  1014. /*
  1015.  * Send a message to all objects inside an object.
  1016.  * Non interactive objects gets no messages.
  1017.  * Compare with say().
  1018.  */
  1019.  
  1020. void tell_room(room, v, avoid)
  1021.     struct object *room;
  1022.     struct svalue *v;
  1023.     struct vector *avoid; /* has to be in alist order */
  1024. {
  1025.     struct object *ob, *save_command_giver = command_giver;
  1026.     char buff[256];
  1027.  
  1028.     switch(v->type) {
  1029.     case T_STRING:
  1030.     strncpy(buff, v->u.string, sizeof buff);
  1031.     buff[sizeof buff - 1] = '\0';
  1032.     break;
  1033.     case T_OBJECT:
  1034.     strncpy(buff, v->u.ob->name, sizeof buff);
  1035.     buff[sizeof buff - 1] = '\0';
  1036.     break;
  1037.     case T_NUMBER:
  1038.     sprintf(buff, "%d", v->u.number);
  1039.     break;
  1040.     default:
  1041.     error("Invalid argument %d to tell_room()\n", v->type);
  1042.     }
  1043.     for (ob = room->contains; ob; ob = ob->next_inv) {
  1044.         int assoc PROT((struct svalue *key, struct vector *));
  1045.     static struct svalue stmp = { T_OBJECT, } ;
  1046.  
  1047.     stmp.u.ob = ob;
  1048.     if (assoc(&stmp, avoid) >= 0) continue;
  1049.     if (ob->interactive == 0) {
  1050.         if (ob->flags & O_ENABLE_COMMANDS) {
  1051.         /*
  1052.          * We want the monster code to have a correct this_player()
  1053.          */
  1054.         command_giver = save_command_giver;
  1055.         tell_npc(ob, buff);
  1056.         }
  1057.         if (ob->flags & O_DESTRUCTED)
  1058.         break;
  1059.         continue;
  1060.     }
  1061.     command_giver = ob;
  1062.     add_message("%s", buff);
  1063.     }
  1064.     command_giver = save_command_giver;
  1065. }
  1066.  
  1067. void shout_string(str)
  1068.     char *str;
  1069. {
  1070.     struct object *ob, *save_command_giver = command_giver;
  1071.     FILE *f = 0;
  1072.     char *p;
  1073.  
  1074.     str = string_copy(str);    /* So that we can modify the string */
  1075.     for (p=str; *p; p++) {
  1076.     if ((*p < ' ' || *p > '~') && *p != '\n')
  1077.         *p = ' ';
  1078.     }
  1079.  
  1080.     p = 0;
  1081. #ifdef LOG_SHOUT
  1082.     if (command_giver) {
  1083.     struct svalue *v;
  1084.     v = apply("query_real_name", command_giver, 0);
  1085.     if (v && v->type == T_STRING)
  1086.         p = v->u.string;
  1087.     } else if (current_object && current_object->user)
  1088.     p = current_object->user->name;
  1089.     if (p)
  1090.     f = fopen("log/SHOUTS", "a");
  1091.     if (f) {
  1092.     fprintf(f, "%s: %s\n", p, str);
  1093.     fclose(f);
  1094.     }
  1095. #endif
  1096.     for (ob = obj_list; ob; ob = ob->next_all) {
  1097.     if (!ob->interactive || ob == save_command_giver || !ob->super)
  1098.         continue;
  1099.     command_giver = ob;
  1100.     add_message("%s", str);
  1101.     }
  1102.     command_giver = save_command_giver;
  1103.     free(str);
  1104. }
  1105.  
  1106. struct object *first_inventory(arg)
  1107.     struct svalue *arg;
  1108. {
  1109.     struct object *ob;
  1110.  
  1111.     if (arg->type == T_STRING)
  1112.     ob = find_object(arg->u.string);
  1113.     else
  1114.     ob = arg->u.ob;
  1115.     if (ob == 0)
  1116.     error("No object to first_inventory()");
  1117.     if (ob->contains == 0)
  1118.     return 0;
  1119.     return ob->contains;
  1120. }
  1121.  
  1122. /*
  1123.  * This will enable an object to use commands normally only
  1124.  * accessible by interactive players.
  1125.  * Also check if the player is a wizard. Wizards must not affect the
  1126.  * value of the wizlist ranking.
  1127.  */
  1128.  
  1129. void enable_commands(num)
  1130.     int num;
  1131. {
  1132.     if (current_object->flags & O_DESTRUCTED)
  1133.     return;
  1134.     if (d_flag > 1) {
  1135.     debug_message("Enable commands %s (ref %d)\n",
  1136.         current_object->name, current_object->ref);
  1137.     }
  1138.     if (num) {
  1139.     current_object->flags |= O_ENABLE_COMMANDS;
  1140.     command_giver = current_object;
  1141.     } else {
  1142.     current_object->flags &= ~O_ENABLE_COMMANDS;
  1143.     command_giver = 0;
  1144.     }
  1145. }
  1146.  
  1147. /*
  1148.  * Set up a function in this object to be called with the next
  1149.  * user input string.
  1150.  */
  1151. int input_to(fun, flag)
  1152.     char *fun;
  1153.     int flag;
  1154. {
  1155.     struct sentence *s;
  1156.  
  1157.     if (!command_giver || command_giver->flags & O_DESTRUCTED)
  1158.     return 0;
  1159.     s = alloc_sentence();
  1160.     if (set_call(command_giver, s, flag)) {
  1161.     s->function = make_shared_string(fun);
  1162.     s->ob = current_object;
  1163.     add_ref(current_object, "input_to");
  1164.     return 1;
  1165.     }
  1166.     free_sentence(s);
  1167.     return 0;
  1168. }
  1169.  
  1170. #define MAX_LINES 50
  1171.  
  1172. /*
  1173.  * This one is used by qsort in get_dir().
  1174.  */
  1175. static int pstrcmp(p1, p2)
  1176.     struct svalue *p1, *p2;
  1177. {
  1178.     return strcmp(p1->u.string, p2->u.string);
  1179. }
  1180.  
  1181. /*
  1182.  * List files in directory. This function do same as standard list_files did,
  1183.  * but instead writing files right away to player this returns an array
  1184.  * containing those files. Actually most of code is copied from list_files()
  1185.  * function.
  1186.  * Differences with list_files:
  1187.  *
  1188.  *   - file_list("/w"); returns ({ "w" })
  1189.  *
  1190.  *   - file_list("/w/"); and file_list("/w/."); return contents of directory
  1191.  *     "/w"
  1192.  *
  1193.  *   - file_list("/");, file_list("."); and file_list("/."); return contents
  1194.  *     of directory "/"
  1195.  */
  1196. struct vector *get_dir(path)
  1197.     char *path;
  1198. {
  1199.     struct vector *v;
  1200.     int i, count = 0;
  1201.     DIR *dirp;
  1202.     int namelen, do_match = 0;
  1203. #if defined(_AIX) || defined(M_UNIX)
  1204.     struct dirent *de;
  1205. #else
  1206.     struct direct *de;
  1207. #endif
  1208.     struct stat st;
  1209.     char *temppath;
  1210.     char *p;
  1211.     char *regexp = 0;
  1212.  
  1213.     if (!path)
  1214.     return 0;
  1215.  
  1216. #ifdef COMPAT_MODE
  1217.     path = check_file_name(path, 0);
  1218. #else
  1219.     path = check_valid_path(path, current_object->eff_user, "get_dir", 0);
  1220. #endif
  1221.  
  1222.     if (path == 0)
  1223.     return 0;
  1224.  
  1225.     /*
  1226.      * We need to modify the returned path, and thus to make a
  1227.      * writeable copy.
  1228.      * The path "" needs 2 bytes to store ".\0".
  1229.      */
  1230.     temppath = (char *)alloca(strlen(path)+2);
  1231.     if (strlen(path)<2) {
  1232.     temppath[0]=path[0]?path[0]:'.';
  1233.     temppath[1]='\000';
  1234.     p = temppath;
  1235.     } else {
  1236.     strcpy(temppath, path);
  1237.     /*
  1238.      * If path ends with '/' or "/." remove it
  1239.      */
  1240.     if ((p = strrchr(temppath, '/')) == 0)
  1241.         p = temppath;
  1242.     if (p[0] == '/' && p[1] == '.' && p[2] == '\0' || 
  1243.         p[0] == '/' && p[1] == '\0')
  1244.         *p = '\0';
  1245.     }
  1246.  
  1247.     if (stat(temppath, &st) < 0) {
  1248.     if (*p == '\0')
  1249.         return 0;
  1250.     regexp = (char *)alloca(strlen(p)+2);
  1251.     if (p != temppath) {
  1252.         strcpy(regexp, p + 1);
  1253.         *p = '\0';
  1254.     } else {
  1255.         strcpy(regexp, p);
  1256.         strcpy(temppath, ".");
  1257.     }
  1258.     do_match = 1;
  1259.     } else if (*p != '\0' && strcmp(temppath, ".")) {
  1260.     if (*p == '/' && *(p + 1) != '\0')
  1261.         p++;
  1262.     v = allocate_array(1);
  1263.     v->item[0].type = T_STRING;
  1264.     v->item[0].string_type = STRING_MALLOC;
  1265.     v->item[0].u.string = string_copy(p);
  1266.     return v;
  1267.     }
  1268.  
  1269.     if ((dirp = opendir(temppath)) == 0)
  1270.     return 0;
  1271.  
  1272.     /*
  1273.      *  Count files
  1274.      */
  1275.     for (de = readdir(dirp); de; de = readdir(dirp)) {
  1276. #ifdef M_UNIX
  1277.     namelen = strlen(de->d_name);
  1278. #else
  1279.     namelen = de->d_namlen;
  1280. #endif
  1281.     if (!do_match && (strcmp(de->d_name, ".") == 0 ||
  1282.               strcmp(de->d_name, "..") == 0))
  1283.         continue;
  1284.     if (do_match && !match_string(regexp, de->d_name))
  1285.         continue;
  1286.     count++;
  1287.     if ( count >= MAX_ARRAY_SIZE)
  1288.         break;
  1289.     }
  1290.     /*
  1291.      * Make array and put files on it.
  1292.      */
  1293.     v = allocate_array(count);
  1294.     if (count == 0) {
  1295.     /* This is the easy case :-) */
  1296.     closedir(dirp);
  1297.     return v;
  1298.     }
  1299.     rewinddir(dirp);
  1300.     for(i = 0, de = readdir(dirp); i < count; de = readdir(dirp)) {
  1301. #ifdef M_UNIX
  1302.         namelen = strlen(de->d_name);
  1303. #else
  1304.     namelen = de->d_namlen;
  1305. #endif
  1306.     if (!do_match && (strcmp(de->d_name, ".") == 0 ||
  1307.               strcmp(de->d_name, "..") == 0))
  1308.         continue;
  1309.     if (do_match && !match_string(regexp, de->d_name))
  1310.         continue;
  1311.     de->d_name[namelen] = '\0';
  1312.     v->item[i].type = T_STRING;
  1313.     v->item[i].string_type = STRING_MALLOC;
  1314.     v->item[i].u.string = string_copy(de->d_name);
  1315.     i++;
  1316.     }
  1317.     closedir(dirp);
  1318.     /* Sort the names. */
  1319.     qsort((char *)v->item, count, sizeof v->item[0], pstrcmp);
  1320.     return v;
  1321. }
  1322.  
  1323. int tail(path)
  1324.     char *path;
  1325. {
  1326.     char buff[1000];
  1327.     FILE *f;
  1328.     struct stat st;
  1329.     int offset;
  1330.  
  1331. #ifdef COMPAT_MODE
  1332.     path = check_file_name(path, 0);
  1333. #else
  1334.     path = check_valid_path(path, current_object->eff_user, "tail", 0);
  1335. #endif
  1336.  
  1337.     if (path == 0)
  1338.         return 0;
  1339.     f = fopen(path, "r");
  1340.     if (f == 0)
  1341.     return 0;
  1342.     if (fstat(fileno(f), &st) == -1)
  1343.     fatal("Could not stat an open file.\n");
  1344.     offset = st.st_size - 54 * 20;
  1345.     if (offset < 0)
  1346.     offset = 0;
  1347.     if (fseek(f, offset, 0) == -1)
  1348.     fatal("Could not seek.\n");
  1349.     /* Throw away the first incomplete line. */
  1350.     if (offset > 0)
  1351.     (void)fgets(buff, sizeof buff, f);
  1352.     while(fgets(buff, sizeof buff, f)) {
  1353.     add_message("%s", buff);
  1354.     }
  1355.     fclose(f);
  1356.     return 1;
  1357. }
  1358.  
  1359. int print_file(path, start, len)
  1360.     char *path;
  1361.     int start, len;
  1362. {
  1363.     char buff[1000];
  1364.     FILE *f;
  1365.     int i;
  1366.  
  1367.     if (len < 0)
  1368.     return 0;
  1369.  
  1370. #ifdef COMPAT_MODE
  1371.     path = check_file_name(path, 0);
  1372. #else
  1373.     path = check_valid_path(path, current_object->eff_user, "print_file", 0);
  1374. #endif
  1375.  
  1376.     if (path == 0)
  1377.         return 0;
  1378.     if (start < 0)
  1379.     return 0;
  1380.     f = fopen(path, "r");
  1381.     if (f == 0)
  1382.     return 0;
  1383.     if (len == 0)
  1384.     len = MAX_LINES;
  1385.     if (len > MAX_LINES)
  1386.     len = MAX_LINES;
  1387.     if (start == 0)
  1388.     start = 1;
  1389.     for (i=1; i < start + len; i++) {
  1390.     if (fgets(buff, sizeof buff, f) == 0)
  1391.         break;
  1392.     if (i >= start)
  1393.         add_message("%s", buff);
  1394.     }
  1395.     fclose(f);
  1396.     if (i <= start)
  1397.     return 0;
  1398.     if (i == MAX_LINES + start)
  1399.     add_message("*****TRUNCATED****\n");
  1400.     return i-start;
  1401. }
  1402.  
  1403. int remove_file(path)
  1404.     char *path;
  1405. {
  1406. #ifdef COMPAT_MODE
  1407.     path = check_file_name(path, 1);
  1408. #else
  1409.     path = check_valid_path(path, current_object->eff_user, "remove_file", 1);
  1410. #endif
  1411.  
  1412.     if (path == 0)
  1413.         return 0;
  1414.     if (unlink(path) == -1)
  1415.         return 0;
  1416.     return 1;
  1417. }
  1418.  
  1419. void log_file(file, str)
  1420.     char *file, *str;
  1421. {
  1422.     FILE *f;
  1423.     char file_name[100];
  1424.     struct stat st;
  1425.  
  1426.     sprintf(file_name, "/log/%s", file);
  1427. #ifdef COMPAT_MODE
  1428.     if (strchr(file, '/') || file[0] == '.' || strlen(file) > 30
  1429. #ifndef MSDOS
  1430.     )
  1431. #else
  1432.     || !valid_msdos(file))
  1433. #endif
  1434.     error("Illegal file name to log_file(%s)\n", file);
  1435. #else
  1436.     if (!check_valid_path(file_name, current_object->eff_user, "log_file", 1))
  1437.         return;
  1438. #endif
  1439.     if (stat(file_name+1, &st) != -1 && st.st_size > MAX_LOG_SIZE) {
  1440.     char file_name2[sizeof file_name + 4];
  1441.     sprintf(file_name2, "%s.old", file_name+1);
  1442.     rename(file_name+1, file_name2);    /* No panic if failure */
  1443.     }
  1444.     f = fopen(file_name+1, "a");    /* Skip leading '/' */
  1445.     if (f == 0)
  1446.     return;
  1447.     fwrite(str, strlen(str), 1, f);
  1448.     fclose(f);
  1449. }
  1450.  
  1451. void
  1452. print_svalue(arg)
  1453.     struct svalue *arg;
  1454. {
  1455.     if (arg == 0)
  1456.     add_message("<NULL>");
  1457.     else if (arg->type == T_STRING) {
  1458.     if (strlen(arg->u.string) > 9500)    /* Not pretty */
  1459.         error("Too long string.\n");
  1460.     /* Strings sent to monsters are now delivered */
  1461.     if (command_giver && (command_giver->flags & O_ENABLE_COMMANDS) &&
  1462.               !command_giver->interactive)
  1463.         tell_npc(command_giver, arg->u.string);
  1464.     else
  1465.         add_message("%s", arg->u.string);
  1466.     } else if (arg->type == T_OBJECT)
  1467.     add_message("OBJ(%s)", arg->u.ob->name);
  1468.     else if (arg->type == T_NUMBER)
  1469.     add_message("%d", arg->u.number);
  1470.     else if (arg->type == T_POINTER)
  1471.     add_message("<ARRAY>");
  1472.     else
  1473.     add_message("<UNKNOWN>");
  1474. }
  1475.  
  1476. void do_write(arg)
  1477.     struct svalue *arg;
  1478. {
  1479.     struct object *save_command_giver = command_giver;
  1480.     if (command_giver == 0 && current_object->shadowing)
  1481.     command_giver = current_object;
  1482.     if (command_giver) {
  1483.     /* Send the message to the first object in the shadow list */
  1484.     while (command_giver->shadowing)
  1485.         command_giver = command_giver->shadowing;
  1486.     }
  1487.     print_svalue(arg);
  1488.     command_giver = save_command_giver;
  1489. }
  1490.  
  1491. /* Find an object. If not loaded, load it !
  1492.  * The object may selfdestruct, which is the only case when 0 will be
  1493.  * returned.
  1494.  */
  1495.  
  1496. struct object *find_object(str)
  1497.     char *str;
  1498. {
  1499.     struct object *ob;
  1500.  
  1501.     /* Remove leading '/' if any. */
  1502.     while(str[0] == '/')
  1503.     str++;
  1504.     ob = find_object2(str);
  1505.     if (ob)
  1506.     return ob;
  1507.     ob = load_object(str, 0);
  1508.     if (ob->flags & O_DESTRUCTED)        /* *sigh* */
  1509.     return 0;
  1510.     if (ob && ob->flags & O_SWAPPED)
  1511.     load_ob_from_swap(ob);
  1512.     return ob;
  1513. }
  1514.  
  1515. /* Look for a loaded object. Return 0 if non found. */
  1516. struct object *find_object2(str)
  1517.     char *str;
  1518. {
  1519.     register struct object *ob;
  1520.     register int length;
  1521.  
  1522.     /* Remove leading '/' if any. */
  1523.     while(str[0] == '/')
  1524.     str++;
  1525.     /* Truncate possible .c in the object name. */
  1526.     length = strlen(str);
  1527.     if (str[length-2] == '.' && str[length-1] == 'c') {
  1528.     /* A new writreable copy of the name is needed. */
  1529.     char *p;
  1530.     p = (char *)alloca(strlen(str)+1);
  1531.     strcpy(p, str);
  1532.     str = p;
  1533.     str[length-2] = '\0';
  1534.     }
  1535.     if (ob = lookup_object_hash(str)) {
  1536.     if (ob->flags & O_SWAPPED)
  1537.         load_ob_from_swap(ob);
  1538.     return ob;
  1539.     }
  1540.     return 0;
  1541. }
  1542.  
  1543. #if 0
  1544.  
  1545. void apply_command(com)
  1546.     char *com;
  1547. {
  1548.     struct value *ret;
  1549.  
  1550.     if (command_giver == 0)
  1551.     error("command_giver == 0 !\n");
  1552.     ret = apply(com, command_giver->super, 0);
  1553.     if (ret != 0) {
  1554.     add_message("Result:");
  1555.     if (ret->type == T_STRING)
  1556.         add_message("%s\n", ret->u.string);
  1557.     if (ret->type == T_NUMBER)
  1558.         add_message("%d\n", ret->u.number);
  1559.     } else {
  1560.     add_message("Error apply_command: function %s not found.\n", com);
  1561.     }
  1562. }
  1563. #endif /* 0 */
  1564.  
  1565. /*
  1566.  * Transfer an object.
  1567.  * The object has to be taken from one inventory list and added to another.
  1568.  * The main work is to update all command definitions, depending on what is
  1569.  * living or not. Note that all objects in the same inventory are affected.
  1570.  *
  1571.  * There are some boring compatibility to handle. When -o flag is specified,
  1572.  * several functions are called in some objects. This is dangerous, as
  1573.  * object might self-destruct when called.
  1574.  */
  1575. void move_object(item, dest)
  1576.     struct object *item, *dest;
  1577. {
  1578.     struct object **pp, *ob, *next_ob;
  1579.     struct object *save_cmd = command_giver;
  1580.  
  1581. #ifndef COMPAT_MODE
  1582.     if (item != current_object)
  1583.     error("Illegal to move other object than this_object()\n");
  1584. #endif
  1585.     /* Recursive moves are not allowed. */
  1586.     for (ob = dest; ob; ob = ob->super)
  1587.     if (ob == item)
  1588.         error("Can't move object inside itself.\n");
  1589.     if (item->shadowing)
  1590.     error("Can't move an object that is shadowing.\n");
  1591.  
  1592. #if 0 /* Not now /Lars */
  1593.     /*
  1594.      * Objects must have inherited std/object if they are to be allowed to
  1595.      * be moved.
  1596.      */
  1597. #ifndef COMPAT_MODE
  1598.     if (!(item->flags & O_APPROVED) ||
  1599.             !(dest->flags & O_APPROVED)) {
  1600.     error("Trying to move object where src or dest not inherit std/object\n");
  1601.     return;
  1602.     }
  1603. #endif    
  1604. #endif
  1605. #ifdef COMPAT_MODE
  1606.     /* This is only needed in -o mode. Otherwise, objects can only move
  1607.      * themselves.
  1608.      */
  1609.     dest->flags &= ~O_RESET_STATE;
  1610.     item->flags &= ~O_RESET_STATE;
  1611. #endif
  1612.     add_light(dest, item->total_light);
  1613.     if (item->super) {
  1614.     int okey = 0;
  1615.         
  1616.     if (item->flags & O_ENABLE_COMMANDS) {
  1617. #ifdef COMPAT_MODE
  1618.         command_giver = item;
  1619.         push_object(item);
  1620.         (void)apply("exit", item->super, 1);
  1621.         if (item->flags & O_DESTRUCTED || dest->flags & O_DESTRUCTED)
  1622.             return;    /* Give up */
  1623. #endif
  1624.         remove_sent(item->super, item);
  1625.     }
  1626.     if (item->super->flags & O_ENABLE_COMMANDS)
  1627.         remove_sent(item, item->super);
  1628.     add_light(item->super, - item->total_light);
  1629.     for (pp = &item->super->contains; *pp;) {
  1630.         if (*pp != item) {
  1631.         if ((*pp)->flags & O_ENABLE_COMMANDS)
  1632.             remove_sent(item, *pp);
  1633.         if (item->flags & O_ENABLE_COMMANDS)
  1634.             remove_sent(*pp, item);
  1635.         pp = &(*pp)->next_inv;
  1636.         continue;
  1637.         }
  1638.         *pp = item->next_inv;
  1639.         okey = 1;
  1640.     }
  1641.     if (!okey)
  1642.         fatal("Failed to find object %s in super list of %s.\n",
  1643.           item->name, item->super->name);
  1644.     }
  1645.     item->next_inv = dest->contains;
  1646.     dest->contains = item;
  1647.     item->super = dest;
  1648.     /*
  1649.      * Setup the new commands. The order is very important, as commands
  1650.      * in the room should override commands defined by the room.
  1651.      * Beware that init() in the room may have moved 'item' !
  1652.      *
  1653.      * The call of init() should really be done by the object itself
  1654.      * (except in the -o mode). It might be too slow, though :-(
  1655.      */
  1656.     if (item->flags & O_ENABLE_COMMANDS) {
  1657.     command_giver = item;
  1658.     (void)apply("init", dest, 0);
  1659.     if ((dest->flags & O_DESTRUCTED) || item->super != dest) {
  1660.         command_giver = save_cmd; /* marion */
  1661.         return;
  1662.     }
  1663.     }
  1664.     /*
  1665.      * Run init of the item once for every present player, and
  1666.      * for the environment (which can be a player).
  1667.      */
  1668.     for (ob = dest->contains; ob; ob=next_ob) {
  1669.     next_ob = ob->next_inv;
  1670.     if (ob == item)
  1671.         continue;
  1672.     if (ob->flags & O_DESTRUCTED)
  1673.         error("An object was destructed at call of init()\n");
  1674.     if (ob->flags & O_ENABLE_COMMANDS) {
  1675.         command_giver = ob;
  1676.         (void)apply("init", item, 0);
  1677.         if (dest != item->super) {
  1678.         command_giver = save_cmd; /* marion */
  1679.         return;
  1680.         }
  1681.     }
  1682.     if (item->flags & O_DESTRUCTED) /* marion */
  1683.         error("The object to be moved was destructed at call of init()\n");
  1684.     if (item->flags & O_ENABLE_COMMANDS) {
  1685.         command_giver = item;
  1686.         (void)apply("init", ob, 0);
  1687.         if (dest != item->super) {
  1688.         command_giver = save_cmd; /* marion */
  1689.         return;
  1690.         }
  1691.     }
  1692.     }
  1693.     if (dest->flags & O_DESTRUCTED) /* marion */
  1694.     error("The destination to move to was destructed at call of init()\n");
  1695.     if (dest->flags & O_ENABLE_COMMANDS) {
  1696.     command_giver = dest;
  1697.     (void)apply("init", item, 0);
  1698.     }
  1699.     command_giver = save_cmd;
  1700. }
  1701.  
  1702. /*
  1703.  * Every object as a count of number of light sources it contains.
  1704.  * Update this.
  1705.  */
  1706.  
  1707. void add_light(p, n)
  1708.     struct object *p;
  1709.     int n;
  1710. {
  1711.     if (n == 0)
  1712.     return;
  1713.     p->total_light += n;
  1714.     if (p->super)
  1715.     add_light(p->super, n);
  1716. }
  1717.  
  1718. struct sentence *sent_free = 0;
  1719. int tot_alloc_sentence;
  1720.  
  1721. struct sentence *alloc_sentence() {
  1722.     struct sentence *p;
  1723.  
  1724.     if (sent_free == 0) {
  1725.     p = (struct sentence *)xalloc(sizeof *p);
  1726.     tot_alloc_sentence++;
  1727.     } else {
  1728.     p = sent_free;
  1729.     sent_free = sent_free->next;
  1730.     }
  1731.     p->verb = 0;
  1732.     p->function = 0;
  1733.     p->next = 0;
  1734.     return p;
  1735. }
  1736.  
  1737. #ifdef free
  1738. void free_all_sent() {
  1739.     struct sentence *p;
  1740.     for (;sent_free; sent_free = p) {
  1741.     p = sent_free->next;
  1742.     free(sent_free);
  1743.     }
  1744. }
  1745. #endif
  1746.  
  1747. void free_sentence(p)
  1748.     struct sentence *p;
  1749. {
  1750.     if (p->function)
  1751.     free_string(p->function);
  1752.     p->function = 0;
  1753.     if (p->verb)
  1754.     free_string(p->verb);
  1755.     p->verb = 0;
  1756.     p->next = sent_free;
  1757.     sent_free = p;
  1758. }
  1759.  
  1760. /*
  1761.  * Find the sentence for a command from the player.
  1762.  * Return success status.
  1763.  */
  1764. int player_parser(buff)
  1765.     char *buff;
  1766. {
  1767.     struct sentence *s;
  1768.     char *p;
  1769.     int length;
  1770.     struct object *save_current_object = current_object;
  1771.     char verb_copy[100];
  1772.  
  1773.     if (d_flag > 1)
  1774.     debug_message("cmd [%s]: %s\n", command_giver->name, buff);
  1775.     /* strip trailing spaces. */
  1776.     for (p = buff + strlen(buff) - 1; p >= buff; p--) {
  1777.     if (*p != ' ')
  1778.         break;
  1779.     *p = '\0';
  1780.     }
  1781.     if (buff[0] == '\0')
  1782.     return 0;
  1783.     if (special_parse(buff))
  1784.     return 1;
  1785.     p = strchr(buff, ' ');
  1786.     if (p == 0)
  1787.     length = strlen(buff);
  1788.     else
  1789.     length = p - buff;
  1790.     clear_notify();
  1791.     for (s=command_giver->sent; s; s = s->next) {
  1792.     struct svalue *ret;
  1793.     int len;
  1794.     struct object *command_object;
  1795.     
  1796.     if (s->verb == 0)
  1797.         error("An 'action' did something, but returned 0 or had an undefined verb.\n");
  1798.     len = strlen(s->verb);
  1799.     if (s->no_space) {
  1800.         if(strncmp(buff, s->verb,len) != 0)
  1801.         continue;
  1802.     } else if (s->short_verb) {
  1803.         if (strncmp(s->verb, buff, len) != 0)
  1804.         continue;
  1805.     } else {
  1806.         if (len != length) continue;
  1807.         if (strncmp(buff, s->verb, length))
  1808.         continue;
  1809.     }
  1810.     /*
  1811.      * Now we have found a special sentence !
  1812.      */
  1813.     if (d_flag > 1)
  1814.         debug_message("Local command %s on %s\n", s->function, s->ob->name);
  1815.     if (length >= sizeof verb_copy)
  1816.         len = sizeof verb_copy - 1;
  1817.     else
  1818.         len = length;
  1819.     strncpy(verb_copy, buff, len);
  1820.     verb_copy[len] = '\0';
  1821.     last_verb = verb_copy;
  1822.     /*
  1823.      * If the function is static and not defined by current object,
  1824.      * then it will fail. If this is called directly from player input,
  1825.      * then we set current_object so that static functions are allowed.
  1826.      * current_object is reset just after the call to apply().
  1827.      */
  1828.     if (current_object == 0)
  1829.         current_object = s->ob;
  1830.     /*
  1831.      * Remember the object, to update score.
  1832.      */
  1833.     command_object = s->ob;
  1834.     if(s->no_space) {
  1835.         push_constant_string(&buff[strlen(s->verb)]);
  1836.         ret = apply(s->function,s->ob, 1);
  1837.     } else if (buff[length] == ' ') {
  1838.         push_constant_string(&buff[length+1]);
  1839.         ret = apply(s->function, s->ob, 1);
  1840.     } else {
  1841.         ret = apply(s->function, s->ob, 0);
  1842.     }
  1843.     if (current_object->flags & O_DESTRUCTED) {
  1844.         /* If disable_commands() were called, then there is no
  1845.          * command_giver any longer.
  1846.          */
  1847.         if (command_giver == 0)
  1848.         return 1;
  1849.         s = command_giver->sent;    /* Restart :-( */
  1850.     }
  1851.     current_object = save_current_object;
  1852.     last_verb = 0;
  1853.     /* If we get fail from the call, it was wrong second argument. */
  1854.     if (ret && ret->type == T_NUMBER && ret->u.number == 0)
  1855.         continue;
  1856.     if (s && command_object->user && command_giver->interactive &&
  1857.         !(command_giver->flags & O_IS_WIZARD))
  1858.     {
  1859.         command_object->user->score++;
  1860.     }
  1861.     if (ret == 0)
  1862.         add_message("Error: function %s not found.\n", s->function);
  1863.     break;
  1864.     }
  1865.     if (s == 0) {
  1866.     notify_no_command();
  1867.     return 0;
  1868.     }
  1869.     return 1;
  1870. }
  1871.  
  1872. /*
  1873.  * Associate a command with function in this object.
  1874.  * The optional second argument is the command name. If the command name
  1875.  * is not given here, it should be given with add_verb().
  1876.  *
  1877.  * The optinal third argument is a flag that will state that the verb should
  1878.  * only match against leading characters.
  1879.  *
  1880.  * The object must be near the command giver, so that we ensure that the
  1881.  * sentence is removed when the command giver leaves.
  1882.  *
  1883.  * If the call is from a shadow, make it look like it is really from
  1884.  * the shadowed object.
  1885.  */
  1886. void add_action(str, cmd, flag)
  1887.     char *str, *cmd;
  1888.     int flag;
  1889. {
  1890.     struct sentence *p;
  1891.     struct object *ob;
  1892.  
  1893.     if (str[0] == ':')
  1894.     error("Illegal function name: %s\n", str);
  1895.     if (current_object->flags & O_DESTRUCTED)
  1896.     return;
  1897.     ob = current_object;
  1898.     while(ob->shadowing)
  1899.     ob = ob->shadowing;
  1900.     if (command_giver == 0 || (command_giver->flags & O_DESTRUCTED))
  1901.     return;
  1902.     if (ob != command_giver && ob->super != command_giver &&
  1903.     ob->super != command_giver->super && ob != command_giver->super)
  1904.       error("add_action from object that was not present.\n");
  1905.     if (d_flag > 1)
  1906.     debug_message("--Add action %s\n", str);
  1907. #ifdef COMPAT_MODE
  1908.     if (strcmp(str, "exit") == 0)
  1909.     error("Illegal to define a command to the exit() function.\n");
  1910. #endif
  1911.     p = alloc_sentence();
  1912.     p->function = make_shared_string(str);
  1913.     p->ob = ob;
  1914.     p->next = command_giver->sent;
  1915.     p->short_verb = flag;
  1916.     p->no_space = 0;
  1917.     if (cmd)
  1918.     p->verb = make_shared_string(cmd);
  1919.     else
  1920.     p->verb = 0;
  1921.     command_giver->sent = p;
  1922. }
  1923.  
  1924. void add_verb(str, no_space)
  1925.     char *str;
  1926.     int no_space;
  1927. {
  1928.     if (command_giver == 0 || (command_giver->flags & O_DESTRUCTED))
  1929.     return;
  1930.     if (command_giver->sent == 0)
  1931.     error("No add_action().\n");
  1932.     if (command_giver->sent->verb != 0)
  1933.     error("Tried to set verb again.\n");
  1934.     command_giver->sent->verb = make_shared_string(str);
  1935.     command_giver->sent->no_space = no_space;
  1936.     if (d_flag > 1)
  1937.     debug_message("--Adding verb %s to action %s\n", str,
  1938.         command_giver->sent->function);
  1939. }
  1940.  
  1941. /*
  1942.  * Remove all commands (sentences) defined by object 'ob' in object
  1943.  * 'player'
  1944.  */
  1945. void remove_sent(ob, player)
  1946.     struct object *ob, *player;
  1947. {
  1948.     struct sentence **s;
  1949.  
  1950.     for (s= &player->sent; *s;) {
  1951.     struct sentence *tmp;
  1952.     if ((*s)->ob == ob) {
  1953.         if (d_flag > 1)
  1954.         debug_message("--Unlinking sentence %s\n", (*s)->function);
  1955.         tmp = *s;
  1956.         *s = tmp->next;
  1957.         free_sentence(tmp);
  1958.     } else
  1959.         s = &((*s)->next);
  1960.     }
  1961. }
  1962.  
  1963. char debug_parse_buff[50]; /* Used for debugging */
  1964.  
  1965. /*
  1966.  * Hard coded commands, that will be available to all players. They can not
  1967.  * be redefined, so the command name should be something obscure, not likely
  1968.  * to be used in the game.
  1969.  */
  1970. int special_parse(buff)
  1971.     char *buff;
  1972. {
  1973. #ifdef DEBUG
  1974.     strncpy(debug_parse_buff, buff, sizeof debug_parse_buff);
  1975.     debug_parse_buff[sizeof debug_parse_buff - 1] = '\0';
  1976. #endif
  1977.     if (strcmp(buff, "malloc") == 0) {
  1978. #if defined(MALLOC_malloc) || defined(MALLOC_smalloc)
  1979.     dump_malloc_data();
  1980. #endif
  1981. #ifdef MALLOC_gmalloc
  1982.     add_message("Using Gnu malloc.\n");
  1983. #endif
  1984. #ifdef MALLOC_sysmalloc
  1985.     add_message("Usage system standard malloc.\n");
  1986. #endif
  1987.     return 1;
  1988.     }
  1989.     if (strcmp(buff, "dumpallobj") == 0) {
  1990.         dumpstat();
  1991.     return 1;
  1992.     }
  1993. #if defined(MALLOC_malloc) || defined(MALLOC_smalloc)
  1994.     if (strcmp(buff, "debugmalloc") == 0) {
  1995.     extern int debugmalloc;
  1996.     debugmalloc = !debugmalloc;
  1997.     if (debugmalloc)
  1998.         add_message("On.\n");
  1999.     else
  2000.         add_message("Off.\n");
  2001.     return 1;
  2002.     }
  2003. #endif
  2004.     if (strcmp(buff, "status") == 0 || strcmp(buff, "status tables") == 0) {
  2005.     int tot, res, verbose = 0;
  2006.     extern char *reserved_area;
  2007.     extern int tot_alloc_sentence, tot_alloc_object,
  2008.         tot_alloc_object_size, num_swapped, total_bytes_swapped,
  2009.         num_arrays, total_array_size;
  2010.     extern int total_num_prog_blocks, total_prog_block_size;
  2011. #ifdef COMM_STAT
  2012.     extern int add_message_calls,inet_packets,inet_volume;
  2013. #endif
  2014.  
  2015.     if (strcmp(buff, "status tables") == 0)
  2016.         verbose = 1;
  2017.     if (reserved_area)
  2018.         res = RESERVED_SIZE;
  2019.     else
  2020.         res = 0;
  2021. #ifdef COMM_STAT
  2022.     if (verbose)
  2023.         add_message("Calls to add_message: %d   Packets: %d   Average packet size: %f\n\n",add_message_calls,inet_packets,(float)inet_volume/inet_packets);
  2024. #endif
  2025.     if (!verbose) {
  2026.         add_message("Sentences:\t\t\t%8d %8d\n", tot_alloc_sentence,
  2027.             tot_alloc_sentence * sizeof (struct sentence));
  2028.         add_message("Objects:\t\t\t%8d %8d\n",
  2029.             tot_alloc_object, tot_alloc_object_size);
  2030.         add_message("Arrays:\t\t\t\t%8d %8d\n", num_arrays,
  2031.             total_array_size);
  2032.         add_message("Prog blocks:\t\t\t%8d %8d (%d swapped, %d Kbytes)\n",
  2033.             total_num_prog_blocks, total_prog_block_size,
  2034.             num_swapped, total_bytes_swapped / 1024);
  2035.         add_message("Memory reserved:\t\t\t %8d\n", res);
  2036.     }
  2037.     if (verbose)
  2038.         stat_living_objects();
  2039.     tot =           total_prog_block_size +
  2040.                total_array_size +
  2041.                tot_alloc_object_size +
  2042.                show_otable_status(verbose) +
  2043.                heart_beat_status(verbose) +
  2044.                add_string_status(verbose) +
  2045.                print_call_out_usage(verbose) +
  2046.                res;
  2047.  
  2048.     if (!verbose) {
  2049.         add_message("\t\t\t\t\t --------\n");
  2050.         add_message("Total:\t\t\t\t\t %8d\n", tot);
  2051.     }
  2052.     return 1;
  2053.     }
  2054.     if (strcmp(buff, "e") == 0) {
  2055.     (void)strcpy(buff, "east");
  2056.     return 0;
  2057.     }
  2058.     if (strcmp(buff, "w") == 0) {
  2059.     (void)strcpy(buff, "west");
  2060.     return 0;
  2061.     }
  2062.     if (strcmp(buff, "s") == 0) {
  2063.     (void)strcpy(buff, "south");
  2064.     return 0;
  2065.     }
  2066.     if (strcmp(buff, "n") == 0) {
  2067.     (void)strcpy(buff, "north");
  2068.     return 0;
  2069.     }
  2070.     if (strcmp(buff, "d") == 0) {
  2071.     (void)strcpy(buff, "down");
  2072.     return 0;
  2073.     }
  2074.     if (strcmp(buff, "u") == 0) {
  2075.     (void)strcpy(buff, "up");
  2076.     return 0;
  2077.     }
  2078.     if (strcmp(buff, "nw") == 0) {
  2079.     (void)strcpy(buff, "northwest");
  2080.     return 0;
  2081.     }
  2082.     if (strcmp(buff, "ne") == 0) {
  2083.     (void)strcpy(buff, "northeast");
  2084.     return 0;
  2085.     }
  2086.     if (strcmp(buff, "sw") == 0) {
  2087.     (void)strcpy(buff, "southwest");
  2088.     return 0;
  2089.     }
  2090.     if (strcmp(buff, "se") == 0) {
  2091.     (void)strcpy(buff, "southeast");
  2092.     return 0;
  2093.     }
  2094.     return 0;
  2095. }
  2096.  
  2097. void print_local_commands() {
  2098.     struct sentence *s;
  2099.  
  2100.     add_message("Current local commands:\n");
  2101.     for (s = command_giver->sent; s; s = s->next)
  2102.     add_message("%s ", s->verb);
  2103.     add_message("\n");
  2104. }
  2105.  
  2106. /*VARARGS1*/
  2107. void fatal(fmt, a, b, c, d, e, f, g, h)
  2108.     char *fmt;
  2109.     int a, b, c, d, e, f, g, h;
  2110. {
  2111.     static int in_fatal = 0;
  2112.     /* Prevent double fatal. */
  2113.     if (in_fatal)
  2114.     abort();
  2115.     in_fatal = 1;
  2116.     (void)fprintf(stderr, fmt, a, b, c, d, e, f, g, h);
  2117.     fflush(stderr);
  2118.     if (current_object)
  2119.     (void)fprintf(stderr, "Current object was %s\n",
  2120.               current_object->name);
  2121.     debug_message(fmt, a, b, c, d, e, f, g, h);
  2122.     if (current_object)
  2123.     debug_message("Current object was %s\n", current_object->name);
  2124.     debug_message("Dump of variables:\n");
  2125.     (void)dump_trace(1);
  2126.     abort();
  2127. }
  2128.  
  2129. int num_error = 0;
  2130.  
  2131. /*
  2132.  * Error() has been "fixed" so that users can catch and throw them.
  2133.  * To catch them nicely, we really have to provide decent error information.
  2134.  * Hence, all errors that are to be caught
  2135.  * (error_recovery_context_exists == 2) construct a string containing
  2136.  * the error message, which is returned as the
  2137.  * thrown value.  Users can throw their own error values however they choose.
  2138.  */
  2139.  
  2140. /*
  2141.  * This is here because throw constructs its own return value; we dont
  2142.  * want to replace it with the system's error string.
  2143.  */
  2144.  
  2145. void throw_error() {
  2146.     extern int error_recovery_context_exists;
  2147.     extern jmp_buf error_recovery_context;
  2148.     if (error_recovery_context_exists > 1) {
  2149.     longjmp(error_recovery_context, 1);
  2150.     fatal("Throw_error failed!");
  2151.     }
  2152.     error("Throw with no catch.\n");
  2153. }
  2154.  
  2155. static char emsg_buf[2000];
  2156.  
  2157. /*VARARGS1*/
  2158. void error(fmt, a, b, c, d, e, f, g, h)
  2159.     char *fmt;
  2160.     int a, b, c, d, e, f, g, h;
  2161. {
  2162.     extern int error_recovery_context_exists;
  2163.     extern jmp_buf error_recovery_context;
  2164.     extern struct object *current_heart_beat;
  2165.     extern struct svalue catch_value;
  2166.     char *object_name;
  2167.  
  2168.     sprintf(emsg_buf+1, fmt, a, b, c, d, e, f, g, h);
  2169.     emsg_buf[0] = '*';    /* all system errors get a * at the start */
  2170.     if (error_recovery_context_exists > 1) { /* user catches this error */
  2171.     struct svalue v;
  2172.     v.type = T_STRING;
  2173.     v.u.string = emsg_buf;
  2174.     v.string_type = STRING_MALLOC;    /* Always reallocate */
  2175.     assign_svalue(&catch_value, &v);
  2176.        longjmp(error_recovery_context, 1);
  2177.        fatal("Catch() longjump failed");
  2178.     }
  2179.     num_error++;
  2180.     if (num_error > 1)
  2181.     fatal("Too many simultaneous errors.\n");
  2182.     debug_message("%s", emsg_buf+1);
  2183.     if (current_object) {
  2184.     debug_message("program: %s, object: %s line %d\n",
  2185.             current_prog ? current_prog->name : "",
  2186.             current_object->name,
  2187.             get_line_number_if_any());
  2188.     if (current_prog)
  2189.         save_error(emsg_buf, current_prog->name,
  2190.                get_line_number_if_any());
  2191.     }
  2192.     object_name = dump_trace(0);
  2193.     fflush(stdout);
  2194.     if (object_name) {
  2195.     struct object *ob;
  2196.     ob = find_object2(object_name);
  2197.     if (!ob) {
  2198.         if (command_giver)
  2199.         add_message("error when executing program in destroyed object %s\n",
  2200.                 object_name);
  2201.         debug_message("error when executing program in destroyed object %s\n",
  2202.             object_name);
  2203.     }
  2204.     }
  2205.     if (command_giver && command_giver->interactive) {
  2206.     struct svalue *v = 0;
  2207.  
  2208.     num_error--;
  2209.     /* 
  2210.      * The stack must be brought in a usable state. After the
  2211.      * call to reset_machine(), all arguments to error() are invalid,
  2212.      * and may not be used any more. The reason is that some strings
  2213.      * may have been on the stack machine stack, and has been deallocated.
  2214.      */
  2215.     reset_machine (0);
  2216.     push_constant_string("error messages");
  2217.     v = apply_master_ob("query_player_level", 1);
  2218.     num_error++;
  2219.     if (v && v->u.number != 0) {
  2220.         add_message("%s", emsg_buf+1);
  2221.         if (current_object)
  2222.         add_message("program: %s, object: %s line %d\n",
  2223.                 current_prog ? current_prog->name : "",
  2224.                 current_object->name,
  2225.                 get_line_number_if_any());
  2226.     } else {
  2227.         add_message("Your sensitive mind notices a wrongness in the fabric of space.\n");
  2228.     }
  2229.     }
  2230.     if (current_heart_beat) {
  2231.     set_heart_beat(current_heart_beat, 0);
  2232.     debug_message("Heart beat in %s turned off.\n",
  2233.               current_heart_beat->name);
  2234.     if (current_heart_beat->interactive) {
  2235.         struct object *save_cmd = command_giver;
  2236.         command_giver = current_heart_beat;
  2237.         add_message("Game driver tells you: You have no heart beat !\n");
  2238.         command_giver = save_cmd;
  2239.     }
  2240.     current_heart_beat = 0;
  2241.     }
  2242.     num_error--;
  2243.     if (error_recovery_context_exists)
  2244.     longjmp(error_recovery_context, 1);
  2245.     abort();
  2246. }
  2247.  
  2248. /*
  2249.  * Check that it is an legal path. No '..' are allowed.
  2250.  */
  2251. int legal_path(path)
  2252.     char *path;
  2253. {
  2254.     char *p;
  2255.  
  2256.     if (path == NULL || strchr(path, ' '))
  2257.     return 0;
  2258.     if (path[0] == '/')
  2259.         return 0;
  2260. #ifdef MSDOS
  2261.     if (!valid_msdos(path)) return(0);
  2262. #endif
  2263.     for(p = strchr(path, '.'); p; p = strchr(p+1, '.')) {
  2264.     if (p[1] == '.')
  2265.         return 0;
  2266.     }
  2267.     return 1;
  2268. }
  2269.  
  2270. /*
  2271.  * There is an error in a specific file. Ask the game driver to log the
  2272.  * message somewhere.
  2273.  */
  2274. void smart_log(error_file, line, what)
  2275.      char *error_file, *what;
  2276.      int line;
  2277. {
  2278.     char buff[500];
  2279.  
  2280.     if (error_file == 0)
  2281.     return;
  2282.     if (strlen(what) + strlen(error_file) > sizeof buff - 100)
  2283.     what = "...[too long error message]...";
  2284.     if (strlen(what) + strlen(error_file) > sizeof buff - 100)
  2285.     error_file = "...[too long filename]...";
  2286.     sprintf(buff, "%s line %d:%s\n", error_file, line, what);
  2287.     push_constant_string(error_file);
  2288.     push_constant_string(buff);
  2289.     apply_master_ob("log_error", 2);
  2290. }
  2291.  
  2292. /*
  2293.  * Check that a file name is valid for read or write.
  2294.  * Also change the name as if the current directory was at the players
  2295.  * own directory.
  2296.  * This is done by functions in the player object.
  2297.  *
  2298.  * The master object (master.c) always have permissions to access
  2299.  * any file in the mudlib hierarchy, but only inside the mudlib.
  2300.  *
  2301.  * WARNING: The string returned will (mostly) be deallocated at next
  2302.  * call of apply().
  2303.  */
  2304. #ifdef COMPAT_MODE
  2305. char debug_check_file[50];
  2306.  
  2307. char *check_file_name(file, writeflg)
  2308.     char *file;
  2309.     int writeflg;
  2310. {
  2311.     struct svalue *ret;
  2312.     char *str;
  2313.  
  2314.     if (current_object != master_ob) {
  2315.     if (!command_giver || !command_giver->interactive ||
  2316.         (command_giver->flags & O_DESTRUCTED))
  2317.         return 0;
  2318.     push_constant_string(file);
  2319.     if (writeflg)
  2320.         ret = apply("valid_write", command_giver, 1);
  2321.     else
  2322.         ret = apply("valid_read",  command_giver, 1);
  2323.     if (command_giver->flags & O_DESTRUCTED)
  2324.         return 0;
  2325.     if (ret == 0 || ret->type != T_STRING) {
  2326.         add_message("Bad file name.\n");
  2327.         return 0;
  2328.     }
  2329.     str = ret->u.string;
  2330.     } else {
  2331.     /* Master object can read/write anywhere ! */
  2332.     if (file[0] == '/')
  2333.         str = file + 1;
  2334.     else
  2335.         str = file;
  2336.     }
  2337.     strncpy(debug_check_file, str, sizeof debug_check_file);
  2338.     debug_check_file[sizeof debug_check_file - 1] = '\0';
  2339.     if (!legal_path(str))
  2340.         error("Illegal path: %s\n", str);
  2341.     /* The string "/" will be converted to "". */
  2342.     if (str[0] == '\0')
  2343.     return ".";
  2344.     return str;
  2345. }
  2346. #endif /* COMPAT_MODE */
  2347.  
  2348. /*
  2349.  * Check that a path to a file is valid for read or write.
  2350.  * This is done by functions in the master object.
  2351.  * The path is always treated as an absolute path, and is returned without
  2352.  * a leading '/'.
  2353.  * If the path was '/', then '.' is returned.
  2354.  * The returned string may or may not be residing inside the argument 'path',
  2355.  * so don't deallocate arg 'path' until the returned result is used no longer.
  2356.  * Otherwise, the returned path is temporarily allocated by apply(), which
  2357.  * means it will be dealocated at next apply().
  2358.  */
  2359. char *check_valid_path(path, eff_user, call_fun, writeflg)
  2360.     char *path;
  2361.     struct wiz_list *eff_user;
  2362.     char *call_fun;
  2363.     int writeflg;
  2364. {
  2365.     struct svalue *v;
  2366.  
  2367.     if (eff_user == 0)
  2368.     return 0;
  2369.     push_string(path, STRING_MALLOC);
  2370.     push_string(eff_user->name, STRING_CONSTANT);
  2371.     push_string(call_fun, STRING_CONSTANT);
  2372.     if (writeflg)
  2373.     v = apply_master_ob("valid_write", 3);
  2374.     else
  2375.     v = apply_master_ob("valid_read", 3);
  2376.     if (v && v->type == T_NUMBER && v->u.number == 0)
  2377.     return 0;
  2378.     if (path[0] == '/')
  2379.     path++;
  2380.     if (path[0] == '\0')
  2381.     path = ".";
  2382.     if (legal_path(path))
  2383.     return path;
  2384.     return 0;
  2385. }
  2386.  
  2387. /*
  2388.  * This one is called from HUP.
  2389.  */
  2390. int game_is_being_shut_down;
  2391.  
  2392. #ifndef _AIX
  2393. void startshutdowngame() {
  2394.     game_is_being_shut_down = 1;
  2395. }
  2396. #else
  2397. void startshutdowngame(int arg) {
  2398.     game_is_being_shut_down = 1;
  2399. }
  2400. #endif
  2401.  
  2402. /*
  2403.  * This one is called from the command "shutdown".
  2404.  * We don't call it directly from HUP, because it is dangerous when being
  2405.  * in an interrupt.
  2406.  */
  2407. void shutdowngame() {
  2408.     shout_string("Game driver shouts: LPmud shutting down immediately.\n");
  2409.     save_wiz_file();
  2410.     ipc_remove();
  2411.     remove_all_players();
  2412.     unlink_swap_file();
  2413. #ifdef DEALLOCATE_MEMORY_AT_SHUTDOWN
  2414.     remove_all_objects();
  2415.     free_all_sent();
  2416.     remove_wiz_list();
  2417.     dump_malloc_data();
  2418.     find_alloced_data();
  2419. #endif
  2420. #ifdef OPCPROF
  2421.     opcdump();
  2422. #endif
  2423.     exit(0);
  2424. }
  2425.  
  2426. /*
  2427.  * Transfer an object from an object to an object.
  2428.  * Call add_weight(), drop(), get(), prevent_insert(), add_weight(),
  2429.  * and can_put_and_get() where needed.
  2430.  * Return 0 on success, and special code on failure:
  2431.  *
  2432.  * 1: To heavy for destination.
  2433.  * 2: Can't be dropped.
  2434.  * 3: Can't take it out of it's container.
  2435.  * 4: The object can't be inserted into bags etc.
  2436.  * 5: The destination doesn't allow insertions of objects.
  2437.  * 6: The object can't be picked up.
  2438.  */
  2439. #ifdef F_TRANSFER
  2440. int transfer_object(ob, to)
  2441.     struct object *ob, *to;
  2442. {
  2443.     struct svalue *v_weight, *ret;
  2444.     int weight;
  2445.     struct object *from = ob->super;
  2446.  
  2447.     /*
  2448.      * Get the weight of the object
  2449.      */
  2450. #ifndef COMPAT_MODE
  2451.     error("transfer() not allowed.\n");
  2452. #endif
  2453.     weight = 0;
  2454.     v_weight = apply("query_weight", ob, 0);
  2455.     if (v_weight && v_weight->type == T_NUMBER)
  2456.     weight = v_weight->u.number;
  2457.     if (ob->flags & O_DESTRUCTED)
  2458.     return 3;
  2459.     /*
  2460.      * If the original place of the object is a living object,
  2461.      * then we must call drop() to check that the object can be dropped.
  2462.      */
  2463.     if (from && (from->flags & O_ENABLE_COMMANDS)) {
  2464.     ret = apply("drop", ob, 0);
  2465.     if (ret && (ret->type != T_NUMBER || ret->u.number != 0))
  2466.         return 2;
  2467.     /* This shold not happen, but we can not trust LPC hackers. :-) */
  2468.     if (ob->flags & O_DESTRUCTED)
  2469.         return 2;
  2470.     }
  2471.     /*
  2472.      * If 'from' is not a room and not a player, check that we may
  2473.      * remove things out of it.
  2474.      */
  2475.     if (from && from->super && !(from->flags & O_ENABLE_COMMANDS)) {
  2476.     ret = apply("can_put_and_get", from, 0);
  2477.     if (!ret || (ret->type != T_NUMBER && ret->u.number != 1) ||
  2478.       (from->flags & O_DESTRUCTED))
  2479.         return 3;
  2480.     }
  2481.     /*
  2482.      * If the destination is not a room, and not a player,
  2483.      * Then we must test 'prevent_insert', and 'can_put_and_get'.
  2484.      */
  2485.     if (to->super && !(to->flags & O_ENABLE_COMMANDS)) {
  2486.     ret = apply("prevent_insert", ob, 0);
  2487.     if (ret && (ret->type != T_NUMBER || ret->u.number != 0))
  2488.         return 4;
  2489.     ret = apply("can_put_and_get", to, 0);
  2490.     if (!ret || (ret->type != T_NUMBER && ret->type != 0) ||
  2491.       (to->flags & O_DESTRUCTED) || (ob->flags & O_DESTRUCTED))
  2492.         return 5;
  2493.     }
  2494.     /*
  2495.      * If the destination is a player, check that he can pick it up.
  2496.      */
  2497.     if (to->flags & O_ENABLE_COMMANDS) {
  2498.     ret = apply("get", ob, 0);
  2499.     if (!ret || (ret->type == T_NUMBER && ret->u.number == 0) ||
  2500.       (ob->flags & O_DESTRUCTED))
  2501.         return 6;
  2502.     }
  2503.     /*
  2504.      * If it is not a room, correct the total weight in the destination.
  2505.      */
  2506.     if (to->super && weight) {
  2507.     /*
  2508.      * Check if the destination can carry that much.
  2509.      */
  2510.     push_number(weight);
  2511.     ret = apply("add_weight", to, 1);
  2512.     if (ret && ret->type == T_NUMBER && ret->u.number == 0)
  2513.         return 1;
  2514.     if (to->flags & O_DESTRUCTED)
  2515.         return 1;
  2516.     }
  2517.     /*
  2518.      * If it is not a room, correct the weight in the 'from' object.
  2519.      */
  2520.     if (from && from->super && weight) {
  2521.     push_number(-weight);
  2522.     (void)apply("add_weight", from, 1);
  2523.     }
  2524.     move_object(ob, to);
  2525.     return 0;
  2526. }
  2527. #endif /* F_TRANSFER */
  2528.  
  2529. /*
  2530.  * Move or destruct one object.
  2531.  */
  2532. void move_or_destruct(what, to)
  2533.     struct object *what, *to;
  2534. {
  2535.     struct svalue v;
  2536.  
  2537. #ifdef COMPAT_MODE
  2538.     int res;
  2539.     res = transfer_object(what, to);
  2540.     if (res == 0 || (what->flags & O_DESTRUCTED))
  2541.     return;
  2542.     if (res == 1 || res == 4 || res == 5) {
  2543.     if (to->super) {
  2544.         move_or_destruct(what, to->super);
  2545.         return;
  2546.     }
  2547.     }
  2548. #else
  2549.     struct svalue *svp;
  2550.     /* This is very dubious, why not just destruct them /JnA 
  2551.         */
  2552.     push_object(to);
  2553.     push_number(1);
  2554.     svp = apply("move", what, 2);
  2555.     if (svp && svp->type == T_NUMBER && svp->u.number == 0)
  2556.     return;
  2557.     if (what->flags & O_DESTRUCTED)
  2558.     return;
  2559. #endif
  2560.     
  2561.     /*
  2562.      * Failed to move the object. Then, it is destroyed.
  2563.      */
  2564.     v.type = T_OBJECT;
  2565.     v.u.ob = what;
  2566.     destruct_object(&v);
  2567. }
  2568.  
  2569. /*
  2570.  * Call this one when there is only little memory left. It will start
  2571.  * Armageddon.
  2572.  */
  2573. void slow_shut_down(minutes)
  2574.     int minutes;
  2575. {
  2576.     struct object *ob;
  2577.  
  2578.     /*
  2579.      * Swap out objects, and free some memory.
  2580.      */
  2581.     ob = find_object("obj/shut");
  2582.     if (!ob) {
  2583.     struct object *save_current = current_object,
  2584.                   *save_command = command_giver;
  2585.     command_giver = 0;
  2586.     current_object = 0;
  2587.     shout_string("Game driver shouts: Out of memory.\n");
  2588.     command_giver = save_command;
  2589.     current_object = save_current;
  2590. #ifndef _AIX
  2591.         startshutdowngame();
  2592. #else
  2593.         startshutdowngame(1);
  2594. #endif
  2595.     return;
  2596.     }
  2597.     shout_string("Game driver shouts: The memory is getting low !\n");
  2598.     push_number(minutes);
  2599.     (void)apply("shut", ob, 1);
  2600. }
  2601.  
  2602. int match_string(match, str)
  2603.     char *match, *str;
  2604. {
  2605.     int i;
  2606.  
  2607.  again:
  2608.     if (*str == '\0' && *match == '\0')
  2609.     return 1;
  2610.     switch(*match) {
  2611.     case '?':
  2612.     if (*str == '\0')
  2613.         return 0;
  2614.     str++;
  2615.     match++;
  2616.     goto again;
  2617.     case '*':
  2618.     match++;
  2619.     if (*match == '\0')
  2620.         return 1;
  2621.     for (i=0; str[i] != '\0'; i++)
  2622.         if (match_string(match, str+i))
  2623.         return 1;
  2624.     return 0;
  2625.     case '\0':
  2626.     return 0;
  2627.     case '\\':
  2628.     match++;
  2629.     if (*match == '\0')
  2630.         return 0;
  2631.     /* Fall through ! */
  2632.     default:
  2633.     if (*match == *str) {
  2634.         match++;
  2635.         str++;
  2636.         goto again;
  2637.     }
  2638.     return 0;
  2639.     }
  2640. }
  2641.  
  2642. /*
  2643.  * Credits for some of the code below goes to Free Software Foundation
  2644.  * Copyright (C) 1990 Free Software Foundation, Inc.
  2645.  * See the GNU General Public License for more details.
  2646.  */
  2647. #ifndef S_ISDIR
  2648. #define    S_ISDIR(m)    (((m)&S_IFMT) == S_IFDIR)
  2649. #endif
  2650.  
  2651. #ifndef S_ISREG
  2652. #define    S_ISREG(m)    (((m)&S_IFMT) == S_IFREG)
  2653. #endif
  2654.  
  2655. int
  2656. isdir (path)
  2657.      char *path;
  2658. {
  2659.   struct stat stats;
  2660.  
  2661.   return stat (path, &stats) == 0 && S_ISDIR (stats.st_mode);
  2662. }
  2663.  
  2664. void
  2665. strip_trailing_slashes (path)
  2666.      char *path;
  2667. {
  2668.   int last;
  2669.  
  2670.   last = strlen (path) - 1;
  2671.   while (last > 0 && path[last] == '/')
  2672.     path[last--] = '\0';
  2673. }
  2674.  
  2675. struct stat to_stats, from_stats;
  2676.  
  2677. int
  2678. copy (from, to)
  2679.      char *from, *to;
  2680. {
  2681.   int ifd;
  2682.   int ofd;
  2683.   char buf[1024 * 8];
  2684.   int len;            /* Number of bytes read into `buf'. */
  2685.   
  2686.   if (!S_ISREG (from_stats.st_mode))
  2687.     {
  2688.       error ("cannot move `%s' across filesystems: Not a regular file\n", from);
  2689.       return 1;
  2690.     }
  2691.   
  2692.   if (unlink (to) && errno != ENOENT)
  2693.     {
  2694.       error ("cannot remove `%s'\n", to);
  2695.       return 1;
  2696.     }
  2697.  
  2698.   ifd = open (from, O_RDONLY, 0);
  2699.   if (ifd < 0)
  2700.     {
  2701.       error ("%s: open failed\n", from);
  2702.       return errno;
  2703.     }
  2704.   ofd = open (to, O_WRONLY | O_CREAT | O_TRUNC, 0600);
  2705.   if (ofd < 0)
  2706.     {
  2707.       error ("%s: open failed\n", to);
  2708.       close (ifd);
  2709.       return 1;
  2710.     }
  2711. #ifndef FCHMOD_MISSING
  2712.   if (fchmod (ofd, from_stats.st_mode & 0777))
  2713.     {
  2714.       error ("%s: fchmod failed\n", to);
  2715.       close (ifd);
  2716.       close (ofd);
  2717.       unlink (to);
  2718.       return 1;
  2719.     }
  2720. #endif
  2721.   
  2722.   while ((len = read (ifd, buf, sizeof (buf))) > 0)
  2723.     {
  2724.       int wrote = 0;
  2725.       char *bp = buf;
  2726.       
  2727.       do
  2728.     {
  2729.       wrote = write (ofd, bp, len);
  2730.       if (wrote < 0)
  2731.         {
  2732.           error ("%s: write failed\n", to);
  2733.           close (ifd);
  2734.           close (ofd);
  2735.           unlink (to);
  2736.           return 1;
  2737.         }
  2738.       bp += wrote;
  2739.       len -= wrote;
  2740.     } while (len > 0);
  2741.     }
  2742.   if (len < 0)
  2743.     {
  2744.       error ("%s: read failed\n", from);
  2745.       close (ifd);
  2746.       close (ofd);
  2747.       unlink (to);
  2748.       return 1;
  2749.     }
  2750.  
  2751.   if (close (ifd) < 0)
  2752.     {
  2753.       error ("%s: close failed", from);
  2754.       close (ofd);
  2755.       return 1;
  2756.     }
  2757.   if (close (ofd) < 0)
  2758.     {
  2759.       error ("%s: close failed", to);
  2760.       return 1;
  2761.     }
  2762.   
  2763. #ifdef FCHMOD_MISSING
  2764.   if (chmod (to, from_stats.st_mode & 0777))
  2765.     {
  2766.       error ("%s: chmod failed\n", to);
  2767.       return 1;
  2768.     }
  2769. #endif
  2770.  
  2771.   return 0;
  2772. }
  2773.  
  2774. /* Move FROM onto TO.  Handles cross-filesystem moves.
  2775.    If TO is a directory, FROM must be also.
  2776.    Return 0 if successful, 1 if an error occurred.  */
  2777.  
  2778. int
  2779. do_move (from, to)
  2780.      char *from;
  2781.      char *to;
  2782. {
  2783.   if (lstat (from, &from_stats) != 0)
  2784.     {
  2785.       error ("%s: lstat failed\n", from);
  2786.       return 1;
  2787.     }
  2788.  
  2789.   if (lstat (to, &to_stats) == 0)
  2790.     {
  2791. #ifndef MSDOS
  2792.       if (from_stats.st_dev == to_stats.st_dev
  2793.       && from_stats.st_ino == to_stats.st_ino)
  2794. #else
  2795.       if (same_file(from,to))
  2796. #endif
  2797.     {
  2798.       error ("`%s' and `%s' are the same file", from, to);
  2799.       return 1;
  2800.     }
  2801.  
  2802.       if (S_ISDIR (to_stats.st_mode))
  2803.     {
  2804.       error ("%s: cannot overwrite directory", to);
  2805.       return 1;
  2806.     }
  2807.  
  2808.     }
  2809.   else if (errno != ENOENT)
  2810.     {
  2811.       error ("%s: unknown error\n", to);
  2812.       return 1;
  2813.     }
  2814. #ifdef SYSV
  2815.   if (isdir(from)) {
  2816.       char cmd_buf[100];
  2817.       sprintf(cmd_buf, "/usr/lib/mv_dir %s %s", from, to);
  2818.       return system(cmd_buf);
  2819.   } else
  2820. #endif /* SYSV */      
  2821.   if (rename (from, to) == 0)
  2822.     {
  2823.       return 0;
  2824.     }
  2825.  
  2826.   if (errno != EXDEV)
  2827.     {
  2828.       error ("cannot move `%s' to `%s'", from, to);
  2829.       return 1;
  2830.     }
  2831.  
  2832.   /* rename failed on cross-filesystem link.  Copy the file instead. */
  2833.  
  2834.   if (copy (from, to))
  2835.       return 1;
  2836.   
  2837.   if (unlink (from))
  2838.     {
  2839.       error ("cannot remove `%s'", from);
  2840.       return 1;
  2841.     }
  2842.  
  2843.   return 0;
  2844.  
  2845. }
  2846.     
  2847. /*
  2848.  * do_rename is used by the efun rename. It is basically a combination
  2849.  * of the unix system call rename and the unix command mv. Please shoot
  2850.  * the people at ATT who made Sys V.
  2851.  */
  2852.  
  2853. #ifdef F_RENAME
  2854. int
  2855. do_rename(fr, t)
  2856.     char *fr, *t;
  2857. {
  2858.     char *from, *to;
  2859.     
  2860.     from = check_valid_path(fr, current_object->eff_user, "do_rename", 1);
  2861.     if(!from)
  2862.     return 1;
  2863.     to = check_valid_path(t, current_object->eff_user, "do_rename", 1);
  2864.     if(!to)
  2865.     return 1;
  2866.     if(!strlen(to) && !strcmp(t, "/")) {
  2867.     to = (char *)alloca(3);
  2868.     sprintf(to, "./");
  2869.     }
  2870.     strip_trailing_slashes (from);
  2871.     if (isdir (to))
  2872.     {
  2873.         /* Target is a directory; build full target filename. */
  2874.         char *cp;
  2875.         char *newto;
  2876.         
  2877.         cp = strrchr (from, '/');
  2878.         if (cp)
  2879.         cp++;
  2880.         else
  2881.         cp = from;
  2882.         
  2883.         newto = (char *) alloca (strlen (to) + 1 + strlen (cp) + 1);
  2884.         sprintf (newto, "%s/%s", to, cp);
  2885.         return do_move (from, newto);
  2886.     }
  2887.     else
  2888.     return do_move (from, to);
  2889. }
  2890. #endif /* F_RENAME */
  2891.